我正在尝试复制为tidyverse软件包找到的“旧” R脚本。
library(dslabs)
DataTib<-as_tibble(us_contagious_diseases)
DataTib_nested <- DataTib %>%
group_by(disease) %>%
nest()
Mean_count_nested <- DataTib_nested %>%
mutate(mean_count = map(.x=DataTib_nested$data, ~mean(.x$count)))
据我了解,我有一个小标题,其中按疾病将数据分组,其余的变量/数据嵌套,然后尝试添加一个新列,该列应表示嵌套的变量“计数”的平均值数据框。
但是我得到了我不太了解的错误:
Error: Problem with `mutate()` input `mean_count`.
x Input `mean_count` can't be recycled to size 1.
i Input `mean_count` is `map(.x = DataTib_nested$data, ~mean(.x$count))`.
i Input `mean_count` must be size 1, not 7.
i The error occured in group 1: disease = "Hepatitis A".
先谢谢您!
答案 0 :(得分:0)
您的语法略有错误:
DataTib_nested <- DataTib %>%
group_by(disease) %>%
nest(data = - disease)
Mean_count_nested <- DataTib_nested %>%
mutate(mean_count = map_dbl(data, ~mean(.x$count)))
请注意,我使用map_dbl
而不是map
,因为返回值是数字。