我可以使用purrr::map()
来获取dataframe
中每一列的均值。我可以结合使用任何地图函数和count()
来获取数据框中每个分类变量的计数吗?
library(dplyr)
library(purrr)
mtcars %>% map(mean)
mtcars %>% mutate(am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))) %>% select_if(is.factor) %>%
map(count)
答案 0 :(得分:2)
您可以使用“表格”功能代替计数:
mtcars %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select_if(is.factor) %>%
map(table)
#$`vs`
#V S
#18 14
#$am
#auto manual
#19 13
答案 1 :(得分:2)
快到了!只需在count
中指定数据:
mtcars %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select_if(is.factor) %>%
map(~count(data.frame(x = .x), x))