如何将列名称传递给函数dplyr

时间:2017-04-16 13:59:17

标签: r function dplyr

我尝试创建一个简单的摘要功能,以加快报告多列数据,以便在R Markdown文件中使用。

var1是数据的分类列,t_var是表示数据四分之一的整数,dt是完整数据。

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

}

说我有一个框架:

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

如果我运行该功能,那么:

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

初始产出很有希望:

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

(注意:在尝试重新创建数据时,出于某种原因我也会收到警告:

未知变量:quarter type, 虽然这不会出现在我的原始数据中)

主要是我收到错误:

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

来自Python,我仍然对如何引用列感到困惑。有人可以解释我如何解决我的错误吗?

1 个答案:

答案 0 :(得分:6)

我们可以使用de 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采用字符串参数,因此可以使用quo_name将quosures转换为字符串。在group_by/summarise/mutate等内部,我们可以通过取消引用(UQ!!

来评估quosure

尽管我们在使用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