I'm trying to add a common prefix to each of the variable names in a data.frame. For example, using the mtcars
data, I could add the prefix "cars." using the following code:
> data(mtcars)
> names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs"
[9] "am" "gear" "carb"
> names(mtcars) <- paste0("cars.", names(mtcars))
> names(mtcars)
[1] "cars.mpg" "cars.cyl" "cars.disp" "cars.hp"
[5] "cars.drat" "cars.wt" "cars.qsec" "cars.vs"
[9] "cars.am" "cars.gear" "cars.carb"
However, I would like to do this as part of a piped operation (i.e., a series of functions strung together using %>%
), using some of the dplyr
syntax. It seems like some combination of rename
and everything()
should do the trick, but I don't know how to make it work. Does anyone have any ideas?