0
votes

I want to make this for loop for each colname in my dataframe but I have an error with group_by method :

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

My code :

for(i in colnames(creditDF)){
 distribution <- creditDF %>%
  group_by(i) %>%
  summarise(value = n()) %>%
  select(label = i, value)

 print(distribution)
}

How can I fix this error?

Thanks for your help.

3
you don't have to loop over the columns simply use summarise_at and pass in the columns you want to summarise - meier_flo
group_by_at will take strings, so that's a possibility if you need the loop. - aosmith
I don't want to do a summarise for each columns I want to automate it - Bourg
group_by_at doesn't work :/ - Bourg

3 Answers

2
votes

I offer a more tidy alternative that creates a frequency table by column and binds them in a single data frame.

library(dplyr)
library(purrr)

mtcars %>%
  map(~table(.x)) %>%
  lapply(as_tibble) %>%
  bind_rows(.id = "var")

# # A tibble: 171 x 3
#     var    .x     n
#   <chr> <chr> <int>
# 1   mpg  10.4     2
# 2   mpg  13.3     1
# 3   mpg  14.3     1
# 4   mpg  14.7     1
# 5   mpg    15     1
# 6   mpg  15.2     2
# 7   mpg  15.5     1
# 8   mpg  15.8     1
# 9   mpg  16.4     1
# 10  mpg  17.3     1
# # ... with 161 more rows
0
votes

If I’m understanding your code correctly You want to find out the unique items in each column in your data frame and print the table to the console

for(i in colnames(creditDF)){
 distribution <- creditDF %>%
  group_by_at(.vars = i) %>%
  summarise(value = n())

 print(distribution)
}
0
votes

Solution with base R.

for(i in creditDF) print(as.data.frame(table(i)))