Adding prefix or suffix to most data.frame variable names in piped R workflow

前端 未结 6 1733
一个人的身影
一个人的身影 2020-12-03 05:07

I want to add a suffix or prefix to most variable names in a data.frame, typically after they\'ve all been transformed in some way and before performing a join. I don\'t ha

6条回答
  •  情书的邮戳
    2020-12-03 06:00

    After additional experimenting since posting this question, I've found that the setNames function will work with the piping as it returns a data.frame:

    dat14 %>%
      group_by(class) %>%
      select(-ID) %>%
      summarise_each(funs(mean(.))) %>%
      setNames(c(names(.)[1], paste0(names(.)[-1],"_mean_2014"))) 
    
      class speed_mean_2014 power_mean_2014 force_mean_2014
    1     a       0.5572500             0.8       0.5519802
    2     b       0.2850798             0.6       1.0888116
    

提交回复
热议问题