How to pass column names to dplyr function

I am trying to create a simple summary function to speed up the reporting of multiple data columns for use in the R Markdown file.

var1 is the categorical data column, t_var is an integer representing a quarter of the data, and dt is the full data.

summarise_data_categorical <- function(var1, t_var, dt){

  print(var1)
  print(t_var)

  #Select the columns to aggregate
  group_func <- dt %>% 
    select(one_of(t_var, var1)) %>%
    group_by(t_var,var1)

  #create simple count summary
  count_table <- group_func %>%
    summarise(count = n()) %>%
    spread(t_var, count)

  #create a frequency version of the same table...
  freq <- dt %>%
    select(t_var, var1) %>%
    group_by(t_var,var1) %>%
    summarise(count = n()) %>%
    mutate(freq = round(count / sum(count),3)*100) %>%
    select(-count)

  #Present that table
  freq_table <- freq %>%
    spread(t_var, freq)

  #Create the chart to do the same thing..
  freq_chart <- freq %>%
    ggplot()+
    geom_line(mapping=aes(x=t_var, y = freq, colour=var1))

  #Compile outputs as a list
  results <- list(count_table, freq_table, freq_chart)

  #Return list
  results

}

Say I have a frame:

fr <- data.frame(lets = sample(LETTERS, 100, replace=TRUE),
           `quarter type` = sample(1:4, 100, replace=TRUE))

If I run the function, then:

summarise_data_categorical("lets", "quarter type", fr)

The primary conclusion is promising:

[1] "lets"
[1] "quarter type"

(NOTE: When I try to recreate the data for any reason, I also get a warning:

Unknown variables: quarter typeAlthough this does not appear in my source data)

The main thing is that I get the error message:

Error in resolve_vars(new_groups, tbl_vars(.data)) : unknown variable to group by : t_var

Coming from Python, I'm still a little confused about how to access columns. Can someone explain how I can fix what I got?

+4
1

quosures devel dplyr ( 0.6.0)

summarise_data_categorical <- function(var1, t_var, dt){

  var1 <- enquo(var1)
  t_var <- enquo(t_var)
  v1 <- quo_name(var1)
  v2 <- quo_name(t_var) 

  dt %>%
    select(one_of(v1, v2)) %>%
    group_by(!!t_var, !!var1) %>%
    summarise(count = n()) 

}
summarise_data_categorical(lets, quartertype, fr)
#Source: local data frame [65 x 3]
#Groups: quartertype [?]

#   quartertype   lets count
#         <int> <fctr> <int>
#1            1      A     1
#2            1      F     2
#3            1      G     2
#4            1      H     1
#5            1      I     1
#6            1      J     4
#7            1      M     3
#8            1      N     1
#9            1      P     1
#10           1      S     5
# ... with 55 more rows

enquo substitute base R, quosures. one_of , quosures quo_name. group_by/summarise/mutate .. quosure unquote (UQ !!)


, quosures dplyr, tidyr.

 summarise_data_categorical <- function(var1, t_var, dt){

  var1 <- enquo(var1)
  t_var <- enquo(t_var)

  v1 <- quo_name(var1)
  v2 <- quo_name(t_var) 

  Summ_func <- dt %>%
                    select(one_of(v1, v2)) %>%
                  group_by(!!t_var, !!var1) %>%
                    summarise(count = n())

   count_table <- Summ_func %>%
                  spread_(v2, "count") 

   freq <-  Summ_func %>%
                  mutate(freq = round(count / sum(count),3)*100) %>%
              select(-count)

   freq_table <- freq %>%
                    spread_(v2, "freq")

   freq_chart <- freq %>%
             ggplot()+
               geom_line(mapping=aes_string(x= v2 , y = "freq", colour= v1)) 

   results <- list(count_table, freq_table, freq_chart)
   results

    }
summarise_data_categorical(lets, quartertype, fr)
#[[1]]
# A tibble: 24 × 5
#     lets   `1`   `2`   `3`   `4`
#*  <fctr> <int> <int> <int> <int>
#1       A    NA    NA     1     2
#2       B     2    NA    NA     1
#3       C     1     5     1     2
#4       E     1     1    NA    NA
#5       G    NA     1     2     2
#6       H     1    NA     1     1
#7       I    NA     1     1     2
#8       J     2     1     1     1
#9       K     1     1     2     1
#10      L    NA     2    NA    NA
# ... with 14 more rows

#[[2]]
# A tibble: 24 × 5
#     lets   `1`   `2`   `3`   `4`
#*  <fctr> <dbl> <dbl> <dbl> <dbl>
#1       A    NA    NA   3.1   9.5
#2       B   8.7    NA    NA   4.8
#3       C   4.3  20.8   3.1   9.5
#4       E   4.3   4.2    NA    NA
#5       G    NA   4.2   6.2   9.5
#6       H   4.3    NA   3.1   4.8
#7       I    NA   4.2   3.1   9.5
#8       J   8.7   4.2   3.1   4.8
#9       K   4.3   4.2   6.2   4.8
#10      L    NA   8.3    NA    NA
## ... with 14 more rows

#[[3]]

enter image description here

+5

Source: https://habr.com/ru/post/1674920/


All Articles