0

I have a data frame that has columns ranging from start_1 to start_32. I'd like to rename all of them to m1 to m32. So far I have done this using the following code:

df %>% rename(m1 = start_1, m2 = start_2, ... m32 = start_32) 

I may need to do this a few more times for other variables and I am looking for more condensed way of achieving the same goal. Thanks!

1 Answer 1

3

You can use names:

names(df) <- gsub("^start_", "m", names(df)) 

or, in dplyr, with rename_with:

library(dplyr) rename_with(df, ~ gsub("start_", "m", .x), starts_with("start_")) rename_with(df, ~ gsub("^start_", "m", .x)) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.