在purr::map()中使用dplyr::count()时出错
在本例中,我想对数据集中的每个字符变量应用count()函数在purr::map()中使用dplyr::count()时出错,r,dplyr,purrr,R,Dplyr,Purrr,在本例中,我想对数据集中的每个字符变量应用count()函数 library(dplyr) library(purrr) nycflights13::flights %>% select_if(is.character) %>% map(., count) 但我收到了错误消息: Error in UseMethod("groups") : no applicable method for 'groups' applied to an object of cl
library(dplyr)
library(purrr)
nycflights13::flights %>%
select_if(is.character) %>%
map(., count)
但我收到了错误消息:
Error in UseMethod("groups") : no applicable method for
'groups' applied to an object of class "character"
我不确定如何解释错误消息或更新代码。类似的代码适用于数值变量,但因子变量会产生与字符变量类似的错误消息
nycflights13::flights %>%
select_if(is.numeric) %>%
map(., mean, na.rm = TRUE)
nycflights13::flights %>%
select_if(is.character) %>%
mutate_all(as.factor) %>%
map(., count)
如果您想要具有值计数的TIBLES列表,可以使用
nycflights13::flights %>%
select_if(is.character) %>%
map(~count(data.frame(x=.x), x))
您希望输出的具体内容是什么
count()
不适用于字符向量——使用count(字母[1:10])时会出现相同的错误。
count设计用于数据帧,而不是向量。@MrFlick我希望查看数据集中每个字符变量的唯一值计数。您需要映射(,表格)
或%%>%count(.)
但它们执行不同的操作,但您期望的是什么类型的数据结构?带有两列(值、计数)的data.frames列表?还有map\u df
以data.frame格式输出map\u df(~count(data.frame(x=),x),.id=“var”)
。使用.id区分变量很方便。