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

Error using dplyr::count() within purrr::map()

在此示例中,我想将 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 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)

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

nycflights13::flights %>% 
  select_if(is.character) %>% 
  map(~count(data.frame(x=.x), x))