Rrogrammatically rename variables?

My data looks something like this:

myData <- data.frame(v1 = rnorm(10), GP1 = rnorm(10), GP2 = rnorm(10))

I would like to rename all the columns that start with GP to originalName_something That is:

myData2 <- data.frame(v1 = rnorm(10), GP1_something = rnorm(10), GP2_something = rnorm(10))

How can i do that? I imagine this should be possible with rename_if and contains but i'm not sure how to do it myData %>% rename_if(contains('GP'), paste0(colnames(.),'_something')) does not work

This kind of does the trick:

myData %>% rename_at(vars(contains('GP')), funs(sub('GP', 'GP_something', .)))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.