【问题标题】:How to create new column using mutate within a custom function如何在自定义函数中使用 mutate 创建新列
【发布时间】:2020-10-06 08:16:06
【问题描述】:

我正在尝试构建一个函数,该函数在 mutate 函数中使用 case_when 转换现有列。最终目标是能够输入表名和列名,然后将字符串附加到现有列名,以便 mutate 函数创建一个新列。谢谢!

my_function <- function(table_name, col_name) {
  table_name %>%
    mutate(paste("new_",col_name) = case_when(as.numeric(col_name) <=4 ~ -1,
                                as.numeric(col_name) > 4 & as.numeric(col_name) <= 8 ~ 0,
                                as.numeric(col_name) > 8 ~ 1))
  }

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    您正沉浸在整洁评估的美妙世界中,您必须ensymenquo 列的名称:

    my_function <- function(table_name, col_name) {
        .col = ensym(col_name)
        table_name %>%
            mutate(!!paste0("new_", col_name) := case_when(as.numeric(!!.col) <=4 ~ -1,
                                                      as.numeric(!!.col) > 4 & as.numeric(!!.col) <= 8 ~ 0,
                                                      as.numeric(!!.col) > 8 ~ 1))
    }
    
    df = tibble(x=1:10)
    my_function(df, "x")
    # A tibble: 10 x 2
           x new_x
       <int> <dbl>
     1     1    -1
     2     2    -1
     3     3    -1
     4     4    -1
     5     5     0
     6     6     0
     7     7     0
     8     8     0
     9     9     1
    10    10     1
    

    您可以在Programming with dplyr 小插图上了解更多信息(包括运算符!!:=)。

    我的示例使用 ensym 并将参数作为字符串,以坚持您的函数。但是,更常见的是使用 enquo 并将参数作为列名:

    my_function2 <- function(table_name, col_name) {
        .col = enquo(col_name)
        table_name %>%
            mutate(!!paste0("new_", quo_name(.col)) := case_when(as.numeric(!!.col) <=4 ~ -1,
                                                           as.numeric(!!.col) > 4 & as.numeric(!!.col) <= 8 ~ 0,
                                                           as.numeric(!!.col) > 8 ~ 1))
    }
    my_function2(df, x) #no quotes on the x!
    # A tibble: 10 x 2
           x new_x
       <int> <dbl>
     1     1    -1
     2     2    -1
     3     3    -1
     4     4    -1
     5     5     0
     6     6     0
     7     7     0
     8     8     0
     9     9     1
    10    10     1
    

    不过,您不应该构建一个接受数字并在 mutate 中使用它的函数吗?这是dplyr v1.0.0 的示例:

    my_function3 = function(x){
        case_when(as.numeric(x) <=4 ~ -1,
                  as.numeric(x) > 4 & as.numeric(x) <= 8 ~ 0,
                  as.numeric(x) > 8 ~ 1)
    }
    df %>% mutate(across(x, my_function3, .names="new_{col}"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多