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

徘徊边缘 提交于 2020-05-13 09:42:08

问题


In this example I want to apply the count() function to every character variable in a dataset.

library(dplyr)
library(purrr)

nycflights13::flights %>% 
    select_if(is.character) %>% 
    map(., count)

But I receive the error message:

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

I'm not sure how to interpret the error message or update my code. Similar code works for numeric variables, but factor variables produce a similar error message to character variables

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

nycflights13::flights %>% 
    select_if(is.character) %>% 
    mutate_all(as.factor) %>% 
    map(., count)

回答1:


If you want a list of tibbles with value counts, you can use

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


来源:https://stackoverflow.com/questions/49989741/error-using-dplyrcount-within-purrrmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!