Paste variable name in mutate (dplyr)

后端 未结 2 958
栀梦
栀梦 2021-02-20 07:47

I try to create a variable with paste() in a mutate_() function (dplyr).

I try to adapt code with this answer (dplyr - mutate: use dynamic variable names) but it doesn\'

2条回答
  •  孤街浪徒
    2021-02-20 08:27

    dplyr 0.7.0 onwards does not require use of mutate_. Here is a solution using := to dynamically assign variable names and helper functions quo name.

    It will be helpful to read vignette("programming", "dplyr") for more info. See also Use dynamic variable names in `dplyr` for older versions of dplyr.

    df <- df %>%
      group_by(segment) %>%
      mutate( !!paste0('MeanSum',quo_name(nameVarPeriod1)) := 
    mean(!!as.name(paste0('Sum',quo_name(nameVarPeriod1)))))
    

    dplyr 1.0.0 alternative:

    Using the new across function in dplyr 1.0.0 we can set names using glue style syntax and can include the function name and original column as part of the name:

    my_fn <- function(nameVarPeriod1 = 'A2'){
      col_list <- paste0('Sum',nameVarPeriod1)
      df %>% 
        group_by(segment) %>%
        mutate(across(col_list, list(mean=mean), .names = "{fn}{col}"))
    }
    
    my_fn()
    #   segment   SumA2 meanSumA2
    #             
    # 1 Seg5    107585.   107585.
    # 2 Seg1    127344.    82080.
    # 3 Seg4    205810.   205810.
    # 4 Seg2    138453.    81528.
    # 5 Seg2     24603.    81528.
    # 6 Seg14    44444.    54422.
    # 7 Seg11   103672    103672 
    # 8 Seg6     88696.    88696.
    # 9 Seg14    64400     54422.
    #10 Seg1     36816.    82080.
    

提交回复
热议问题