将 gt_sparkline() 应用于具有 NA 的多个数字列会产生错误
Apply gt_sparkline() to multiple numeric columns with NAs generates error
给定样本数据如下:
df <- structure(list(variable = c("cabbage", "radish", "apple", "pear",
"monitor", "mouse", "camera", "calculator"), X2021.q1 = c(6,
5, 6, 2.9, 2000, NA, 600, 35), X2021.q2 = c(4L, 8L, 4L, NA, 2001L,
11L, 601L, NA), X2021.q3 = c(8, NA, 5, 4.9, 2002, 12, 602, 37
), X2021.q4 = c(8L, 3L, 2L, 7L, NA, 10L, 580L, 33L)), class = "data.frame", row.names = c(NA,
-8L))
输出:
我正在尝试使用以下方法为每个 variable
绘制时间序列:
df %>%
mutate(data=list(c_across(where(is.numeric)))) %>%
gt() %>%
gt_sparkline(data)
但它引发了一个错误:Error in if (med_y_rnd > 0) { : missing value where TRUE/FALSE needed
。
我该如何解决这个问题?谢谢。
引用link:
我认为问题是由 NA
引起的。
在管道中尝试 na.omit()
,
df %>%
na.omit() %>%
mutate(data=list(c_across(where(is.numeric)))) %>%
gt() %>%
gt_sparkline(data)
给定样本数据如下:
df <- structure(list(variable = c("cabbage", "radish", "apple", "pear",
"monitor", "mouse", "camera", "calculator"), X2021.q1 = c(6,
5, 6, 2.9, 2000, NA, 600, 35), X2021.q2 = c(4L, 8L, 4L, NA, 2001L,
11L, 601L, NA), X2021.q3 = c(8, NA, 5, 4.9, 2002, 12, 602, 37
), X2021.q4 = c(8L, 3L, 2L, 7L, NA, 10L, 580L, 33L)), class = "data.frame", row.names = c(NA,
-8L))
输出:
variable
绘制时间序列:
df %>%
mutate(data=list(c_across(where(is.numeric)))) %>%
gt() %>%
gt_sparkline(data)
但它引发了一个错误:Error in if (med_y_rnd > 0) { : missing value where TRUE/FALSE needed
。
我该如何解决这个问题?谢谢。
引用link:
我认为问题是由 NA
引起的。
在管道中尝试 na.omit()
,
df %>%
na.omit() %>%
mutate(data=list(c_across(where(is.numeric)))) %>%
gt() %>%
gt_sparkline(data)