【问题标题】:Using using variable names in functions when using mutate_at使用 mutate_at 时在函数中使用变量名
【发布时间】:2019-04-23 20:26:17
【问题描述】:

我想创建一个函数,我可以在其中根据用户提供的输入变量和截止值列表以编程方式添加变量。

具体来说,我想定义一个函数

myfun <- function(df, varlist, cutofflist)

返回df 并为varlist 中的每个变量添加一列,其中包含每个变量是否最多为相应的截止值的逻辑。

例如,假设我们取 iris 数据帧,

df <- as_tibble(iris)
# A tibble: 150 x 1
   iris$Sepal.Length $Sepal.Width $Petal.Length $Petal.Width $Species
               <dbl>        <dbl>         <dbl>        <dbl> <fct>   
 1               5.1          3.5           1.4          0.2 setosa  
 2               4.9          3             1.4          0.2 setosa  
 3               4.7          3.2           1.3          0.2 setosa  
 4               4.6          3.1           1.5          0.2 setosa  
 5               5            3.6           1.4          0.2 setosa  
 6               5.4          3.9           1.7          0.4 setosa  
 7               4.6          3.4           1.4          0.3 setosa  
 8               5            3.4           1.5          0.2 setosa  
 9               4.4          2.9           1.4          0.2 setosa  
10               4.9          3.1           1.5          0.1 setosa  
# ... with 140 more rows

我想接电话

myfun(df, c("Sepal.Length", "Petal.Length"), list(Sepal.Length = 5, Petal.Length = 1.5))

产生与

相同的结果
df %>%
   mutate(
      Sepal.Length_indicator = (Sepal.Length <= 5),
      Petal.Length_indicator = (Petal.Length <= 1.5)
   )

即这个:

# A tibble: 150 x 7
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_indicator Petal.Length_indicator
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <lgl>                  <lgl>                 
 1          5.1         3.5          1.4         0.2 setosa  FALSE                  TRUE                  
 2          4.9         3            1.4         0.2 setosa  TRUE                   TRUE                  
 3          4.7         3.2          1.3         0.2 setosa  TRUE                   TRUE                  
 4          4.6         3.1          1.5         0.2 setosa  TRUE                   TRUE                  
 5          5           3.6          1.4         0.2 setosa  TRUE                   TRUE                  
 6          5.4         3.9          1.7         0.4 setosa  FALSE                  FALSE                 
 7          4.6         3.4          1.4         0.3 setosa  TRUE                   TRUE                  
 8          5           3.4          1.5         0.2 setosa  TRUE                   TRUE                  
 9          4.4         2.9          1.4         0.2 setosa  TRUE                   TRUE                  
10          4.9         3.1          1.5         0.1 setosa  TRUE                   TRUE                  
# ... with 140 more rows

我对 dplyr 使用 quosures 等还是很陌生。到目前为止,我正在尝试以下内容:

myfun <- function(df, varlist, cutofflist){
  df %>%
    mutate_at(.vars = varlist, .funs = list(indicator = function(x) x<= cutofflist[[?]]))
}

但我不知道应该用什么替换上面的?。如果所有变量的截止值都相同,则该解决方案有效,但如果截止值取决于变量,则该解决方案无效。

提前感谢您的帮助。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    这是map2 和transmute 的一个选项

    library(tidyverse)
    myfun <- function(data, varVec, cutofflist) {
        map2_dfc(varVec, cutofflist[varVec], ~   
    
                         data %>% 
                            transmute( !! paste0(.x, "_indicator") := 
                                   !! rlang::sym(.x) <= .y)) %>%
                     bind_cols(df, .)
    
         }
    
    
    out2 <- myfun(df, c("Sepal.Length", "Petal.Length"), 
            list(Sepal.Length = 5, Petal.Length = 1.5))   
    

    -通过在函数外运行来检查输出

    out1 <- df %>%
             mutate(
               Sepal.Length_indicator = (Sepal.Length <= 5),
               Petal.Length_indicator = (Petal.Length <= 1.5)
              )    
    
    identical(out1, out2)
    #[1] TRUE
    

    或者也可以使用 map 来完成,因为 'varVec' 和 'cutofflist' 名称相同

    myfun <- function(data, varVec, cutofflist) {
        map_dfc(varVec, ~   
    
                            data %>% 
                                      transmute( !! paste0(.x, "_indicator") := 
                !! rlang::sym(.x) <= cutofflist[[.x]])
    
    
    
                                     ) %>%
               bind_cols(df, .)
    
                                     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-03
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2019-10-23
      • 2017-01-05
      相关资源
      最近更新 更多