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)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With