[英]apply count() to every factor variable in a dataframe
I can use purrr::map()
to get the mean of every column in a dataframe
. 我可以使用
purrr::map()
来获取dataframe
中每一列的均值。 Can I use any of the map functions in combination with count()
to get counts for each categorical variable in a 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)
You can use the 'table' function instead of count: 您可以使用“表格”功能代替计数:
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
Almost there! 快好了! Just need to specify the data in
count
: 只需指定
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.