Package: dplyr


Function: rename_with()


  • Note: Unlike dplyr::rename() where you can rename individual columns, dplyr::rename_with() renames columns using a function.


1. Concatenate a prefix, “T1_”, to all variable names

Review the data (d1)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         2   3.6
2 b        NA   8.5
3 c         3  NA  

Concatenate T1_ by adding the base::paste0 function within the dplyr::rename_with() function.

  • Note: Using base::paste0 concatenates the string directly to the variable name. Using base::paste() or stringr::str_c() concatenates with a string separator.
d1 %>% 
  dplyr::rename_with(~ base::paste0("T1_", .))
# A tibble: 3 x 3
  T1_Var1 T1_Var2 T1_Var3
  <chr>     <dbl>   <dbl>
1 a             2     3.6
2 b            NA     8.5
3 c             3    NA  

To make a suffix _T1 rather than a prefix, rewrite the function like this.

d1 %>% 
  dplyr::rename_with(~ base::paste0(., "_T1"))
# A tibble: 3 x 3
  Var1_T1 Var2_T1 Var3_T1
  <chr>     <dbl>   <dbl>
1 a             2     3.6
2 b            NA     8.5
3 c             3    NA  

You can also use the base::paste() function or the stringr::str_c() function (they are equivalent) instead and add your separator using the sep argument.

d1 %>% 
  dplyr::rename_with(~ base::paste(., "T1", sep= "_"))
# A tibble: 3 x 3
  Var1_T1 Var2_T1 Var3_T1
  <chr>     <dbl>   <dbl>
1 a             2     3.6
2 b            NA     8.5
3 c             3    NA  

2. Concatenate a prefix, “T1_”, to only numeric variable names

Review the data (d1)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         2   3.6
2 b        NA   8.5
3 c         3  NA  

Concatenate T1_ by adding the base::paste0 function.

If we want to select specific variables, we can add them to the dplyr::rename_with() argument .cols. Here we are going to select columns using the selection helper where().

  • Note: Other tidyselect selection helpers include starts_with(), ends_with(), contains(), matches(), and num_range(), and more.

  • Note: You must wrap is.numeric, a predicate function (returns a true/false), in where().

d1 %>% 
  dplyr::rename_with(~ base::paste0("T1_", .), .cols = where(is.numeric))
# A tibble: 3 x 3
  Var1  T1_Var2 T1_Var3
  <chr>   <dbl>   <dbl>
1 a           2     3.6
2 b          NA     8.5
3 c           3    NA  

3. Concatenate a prefix, “T1_”, to only Var2 and Var3

Review the data (d1)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         2   3.6
2 b        NA   8.5
3 c         3  NA  

Concatenate T1_ by adding the base::paste0 function.

d1 %>% 
  dplyr::rename_with(~ base::paste0("T1_", .), .cols = Var2:Var3)
# A tibble: 3 x 3
  Var1  T1_Var2 T1_Var3
  <chr>   <dbl>   <dbl>
1 a           2     3.6
2 b          NA     8.5
3 c           3    NA  

I could also do this by choose all variables except Var1.

d1 %>% 
  dplyr::rename_with(~ base::paste0("T1_", .), .cols = -Var1)
# A tibble: 3 x 3
  Var1  T1_Var2 T1_Var3
  <chr>   <dbl>   <dbl>
1 a           2     3.6
2 b          NA     8.5
3 c           3    NA  

4. Concatenate a prefix, “T1_”, to Var1 to Var3 and Var5

Review the data (d8)

# A tibble: 3 x 5
  Var1   Var2  Var3  Var4  Var5
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a         2   3.6     1     0
2 b        NA   8.5     2     1
3 c         3  NA       3     1

Concatenate “T1_” by adding the base::paste0 function.

d8 %>% 
  dplyr::rename_with(~ base::paste0("T1_", .), .cols = c(Var1:Var3, Var5))
# A tibble: 3 x 5
  T1_Var1 T1_Var2 T1_Var3  Var4 T1_Var5
  <chr>     <dbl>   <dbl> <dbl>   <dbl>
1 a             2     3.6     1       0
2 b            NA     8.5     2       1
3 c             3    NA       3       1

Package: purrr


Function: set_names()


  • Note: You can use purrr::set_names() to concatenate to variable names as well, but it doesn’t have the nice compatibility with tidyselect that allows you to modify only select variable names. But purrr::set_names() works well to modify all variable names.


1. Concatenate a prefix, “T1_”, to all variable names

Review the data (d1)

# A tibble: 3 x 3
  Var1   Var2  Var3
  <chr> <dbl> <dbl>
1 a         2   3.6
2 b        NA   8.5
3 c         3  NA  

Concatenate “T1_” by adding the stringr::str_c() function.

  • Note: Here we can add the argument *sep = “_“* to add an underscore after T1.
d1 %>% 
  purrr::set_names(~ stringr::str_c("T1", sep = "_", .))
# A tibble: 3 x 3
  T1_Var1 T1_Var2 T1_Var3
  <chr>     <dbl>   <dbl>
1 a             2     3.6
2 b            NA     8.5
3 c             3    NA  

Return to Name Variables