base :: noquote是使用dplyr进行编程的公认方法吗?

时间:2019-03-07 15:46:31

标签: r dplyr tidyr tidyeval

我有一个函数需要一个数据框,并且必须根据传入的数据集根据不同的列来收集它。因此,我需要一种方法来告诉gather()函数需要动态收集哪些列。我很好奇,如果将列作为字符串传递给函数,然后使用noqoute()是执行此操作的最佳方法。该代码当前有效,但是我觉得我可能遗漏了一些可能最终破坏代码的东西。这是我的代码:

library(tidyverse)

# Data will always take a wide format similar to this
raw_dat <- tibble(id = 1:30, 
       garbage = letters[runif(30, min = 1, max = 26)], 
       group1 = runif(30, min = 200, max = 5000),
       group2 = runif(30, min = 200, max = 5000),
       group3 = runif(30, min = 200, max = 5000),
       group4 = runif(30, min = 200, max = 5000),
       group5 = runif(30, min = 200, max = 5000),
       garbage2 = letters[runif(30, min = 1, max = 26)],
       garbage3 = letters[runif(30, min = 1, max = 26)])

raw_dat
#> # A tibble: 30 x 9
#>       id garbage group1 group2 group3 group4 group5 garbage2 garbage3
#>    <int> <chr>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <chr>    <chr>   
#>  1     1 n        2234.  4852.  4745.  4381.  3331. v        l       
#>  2     2 y        3749.  2084.  2555.  3445.  3288. y        e       
#>  3     3 x        1796.  1088.  2300.  2425.  2124. u        j       
#>  4     4 m        3912.   646.   497.  4669.  2532. g        c       
#>  5     5 y         344.   559.  1989.  4459.  4139. x        m       
#>  6     6 k        4671.  1215.  1568.  3802.  1536. y        p       
#>  7     7 g        3117.  3797.   391.  3192.  2695. t        l       
#>  8     8 x         230.  1277.  4450.  3437.  1235. c        o       
#>  9     9 x         491.   871.  4559.  4010.  4478. b        r       
#> 10    10 l         292.   701.   903.  2308.  4573. e        e       
#> # … with 20 more rows

# This number will change by varying datasets and user entry
input_groups <- names(raw_dat)[3:7]

# My function does more than this but I am concerned with making this part dynamic
compute_values <- function(df, input_g){
  df %>% 
    gather(key = "input_group", value = "input_value", noquote(input_g))
}

compute_values(raw_dat, input_groups)
#> # A tibble: 150 x 6
#>       id garbage garbage2 garbage3 input_group input_value
#>    <int> <chr>   <chr>    <chr>    <chr>             <dbl>
#>  1     1 n       v        l        group1            2234.
#>  2     2 y       y        e        group1            3749.
#>  3     3 x       u        j        group1            1796.
#>  4     4 m       g        c        group1            3912.
#>  5     5 y       x        m        group1             344.
#>  6     6 k       y        p        group1            4671.
#>  7     7 g       t        l        group1            3117.
#>  8     8 x       c        o        group1             230.
#>  9     9 x       b        r        group1             491.
#> 10    10 l       e        e        group1             292.
#> # … with 140 more rows

reprex package(v0.2.1)于2019-03-07创建

0 个答案:

没有答案