0

I have some question for programming using dplyr and for loop in order to create multiple data. The code without loop works very well, but the code with for loop doesn't give me the expected result as well as error message.

Error message was like:

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

Please anyone put me on the right way.

The code below worked

B <- data %>% select (column1) %>% group_by (column1) %>% arrange (column1) %>% summarise (n = n ())

The code below did not work

column_list <- c ('column1', 'column2', 'column3')

for (b in column_list) {

 a <- data %>% select (b) %>% group_by (b) %>% arrange (b) %>% summarise (n = n () )
 assign (paste0(b), a)
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

1

Don't use assign. Instead use lists.

We can use _at variations in dplyr which works with characters variables.

library(dplyr)

split_fun <- function(df, col) {
  df %>% group_by_at(col) %>% summarise(n = n()) %>% arrange_at(col)
}

and then use lapply/map to apply it to different columns

purrr::map(column_list, ~split_fun(data, .))

This will return you a list of dataframes which can be accessed using [[ individually if needed.


Using example with mtcars

df <- mtcars
column_list <- c ('cyl', 'gear', 'carb')

purrr::map(column_list, ~split_fun(df, .))

#[[1]]
# A tibble: 3 x 2
#    cyl     n
#  <dbl> <int>
#1     4    11
#2     6     7
#3     8    14

#[[2]]
# A tibble: 3 x 2
#   gear     n
#  <dbl> <int>
#1     3    15
#2     4    12
#3     5     5

#[[3]]
# A tibble: 6 x 2
#   carb     n
#  <dbl> <int>
#1     1     7
#2     2    10
#3     3     3
#4     4    10
#5     6     1
#6     8     1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your comment. Your code works very well. I have one more question. Could you give me additional code for assigning those table in each different data? – Sangwon Steve Lee Jun 27 '19 at 05:34
  • @SangwonSteveLee It is already there. If you assign the value `l1 <- purrr::map(column_list, ~split_fun(data, .))`. You can then use each one of them separately. `l1[[1]]`, `l1[[2]]` and so on. – Ronak Shah Jun 27 '19 at 06:01
  • 1
    Thanks for your kind and clear explanation. Your solution works very well. – Sangwon Steve Lee Jun 27 '19 at 06:10