I want to add a suffix or prefix for most variable names in data.frame, usually after all of them have been somehow converted and before the connection is made. I have no way to do this without breaking my pipeline.
For example, with this data:
library(dplyr) set.seed(1) dat14 <- data.frame(ID = 1:10, speed = runif(10), power = rpois(10, 1), force = rexp(10), class = rep(c("a", "b"),5))
I want to get this result (variable note names):
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
My current approach:
means14 <- dat14 %>% group_by(class) %>% select(-ID) %>% summarise_each(funs(mean(.))) names(means14)[2:length(names(means14))] <- paste0(names(means14)[2:length(names(means14))], "_mean_2014")
Is there an alternative to this awkward last line that breaks my pipes? I looked at select() and rename() , but I donโt want to explicitly specify the name of each variable, since I usually want to rename everything except one variable, and can have a much wider data format than in this example.
I present the last command that approximates this created function:
appendname(cols = 2:n, str = "_mean_2014", placement = "suffix")
What does not exist, as far as I know.