Paste variable name in mutate (dplyr)

后端 未结 2 979
栀梦
栀梦 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:43

    Not sure what is your purpose of renaming summarized column name with original column names. But if you are looking for a solution where you want to have sum of multiple columns and hence wants to rename those then dplyr::mutate_at does it for you.

    library(dplyr)
    df %>% group_by(segment) %>%
      mutate(SumA3 = SumA2) %>%     #Added another column to demonstrate 
      mutate_at(vars(starts_with("SumA")), funs(mean = "mean"))
    
    #  segment  SumA2  SumA3 SumA2_mean SumA3_mean
    #                  
    # 1 Seg5    107585 107585     107585     107585
    # 2 Seg1    127344 127344      82080      82080
    # 3 Seg4    205810 205810     205810     205810
    # 4 Seg2    138453 138453      81528      81528
    # 5 Seg2     24603  24603      81528      81528
    # 6 Seg14    44444  44444      54422      54422
    # 7 Seg11   103672 103672     103672     103672
    # 8 Seg6     88696  88696      88696      88696
    # 9 Seg14    64400  64400      54422      54422
    # 10 Seg1     36816  36816      82080      82080
    

提交回复
热议问题