在purrr :: map()中使用dplyr :: count()时出错

Joe*_*Joe 3 r dplyr purrr

在这个例子中,我想将count()函数应用于数据集中的每个字符变量.

library(dplyr)
library(purrr)

nycflights13::flights %>% 
    select_if(is.character) %>% 
    map(., count)
Run Code Online (Sandbox Code Playgroud)

但是我收到错误消息:

Error in UseMethod("groups") : no applicable method for 
'groups' applied to an object of class "character"
Run Code Online (Sandbox Code Playgroud)

我不确定如何解释错误消息或更新我的代码.类似的代码适用于数字变量,但因子变量会对字符变量产生类似的错误消息

nycflights13::flights %>% 
    select_if(is.numeric) %>% 
    map(., mean, na.rm = TRUE)

nycflights13::flights %>% 
    select_if(is.character) %>% 
    mutate_all(as.factor) %>% 
    map(., count)
Run Code Online (Sandbox Code Playgroud)

MrF*_*ick 9

如果你想要一个带有值计数的元组列表,你可以使用

nycflights13::flights %>% 
  select_if(is.character) %>% 
  map(~count(data.frame(x=.x), x))
Run Code Online (Sandbox Code Playgroud)