nstein1
nstein1

Reputation: 31

Is there an elegant tidyverse method for renaming columns en masse?

I'm working with a small dataset built from a Google Form. The column names are the full survey questions eg.

"1. What team most describes your work?"
"2. Are you a manager?"
"3. How long have you been with x?"

I want to rename all of these columns "1", "2", "3" etc. I know that I can do the below, but I'm hoping there's a more elegant/quicker way. There are 23 columns like this.

survey %>% rename_with(
  `1` = `1. What team most describes your work?`,
  `2` = `2. Are you a manager?`...

This works, but is slow to compose. Anyone have anything better in tidyverse? I'm new to R and coding generally, so any tips appreciated.

Upvotes: 3

Views: 782

Answers (2)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21440

There are more concise solutions possible. One of them is with str_extract:

library(stringr)
df %>% rename_with(~str_extract(., "\\d+"))

Here we extract the first digit in the names strings.

Another is with str_remove:

df %>% rename_with(~str_remove(., "\\..*"))

Here we remove anything from the period onwards.

Data (thanks to @benson):

df <- tibble("1. What team most describes your work?" = "1",
             "2. Are you a manager?" = "2",
             "3. How long have you been with x?" = "3")

Upvotes: 4

benson23
benson23

Reputation: 19142

You can supply multiple columns in rename_with(.cols = your_columns), and use a function to apply to the selected columns. Here, the question number (the digit before the dot) is captured ((\\d+)) and the whole column name is replaced by that digit (regex capture group 1 \\1).

If you omit the .cols argument, all columns will be selected by default.

library(dplyr)

# dummy df
df <- tibble("1. What team most describes your work?" = "1",
             "2. Are you a manager?" = "2",
             "3. How long have you been with x?" = "3")

df %>% rename_with(~sub("(\\d+)\\..*$", "\\1", .x))

# A tibble: 1 × 3
  `1`   `2`   `3`  
  <chr> <chr> <chr>
1 1     2     3    

Upvotes: 6

Related Questions