8

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)
Joe
  • 3,217
  • 3
  • 21
  • 37
  • 2
    What exactly do you expect the output to be? `count()` isn't meant to be used on character vectors -- you get the same error with `count(letters[1:10])`. – MrFlick Apr 23 '18 at 20:54
  • 2
    `count` is designed to work on a data frame, not a vector. – alistaire Apr 23 '18 at 20:54
  • @MrFlick I was hoping to view the counts of unique values for each character variable in my dataset. – Joe Apr 23 '18 at 20:55
  • 1
    You either want `map(., table)` or `%>% count(.)` but they perform different things – CPak Apr 23 '18 at 20:56
  • 1
    But what type of data structure were you expecting? A list of data.frames with two columns (value, count)? – MrFlick Apr 23 '18 at 20:57
  • @MrFlick I was expecting a list of data.frames, but using the table() function as suggested by CPak is producing a named vector that can be converted to data.frames – Joe Apr 23 '18 at 20:59

1 Answers1

15

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))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    There's also `map_df` to output in data.frame format `map_df(~ count(data.frame(x = .), x), .id = "var")`. It's handy to use .id to distinguish between variables. – pasipasi May 03 '19 at 09:19
  • hey! @MrFlick, thank you for this answer. But can you help me explain how this works? For reading it, it appears you are first creating a data frame by extract each column of the source data frame but putting it in a single column. Then passing this single column dataframe into count. However why doesn't `map(~count(.))` work by itself? Super curious – alejandro_hagan Mar 22 '22 at 00:06
  • @alejandro_hagan The `count()` function is a dplyr function that expects a data.frame/tibble as the first parameter. When you use `map()` with a data.frame/tibble, you are mapping over the column values which are not tibbles. So if you want to use count, you need to turn those vectors into data.frames/tibbles. – MrFlick Mar 22 '22 at 05:52
  • thank you so much @MrFlick! I understand it much better now. I literally could not figure it out. I appreciate your response! – alejandro_hagan Mar 24 '22 at 23:17