In this tutorial we will learn how to add a prefix or a suffix to column names of a data frame in R. We will use dplyr’s rename_with() function to add prefix/suffix to column names. dplyr’s rename_with() function belongs to another renaming function in dplyr, rename() that is useful. for renaming a individual columns.
First, we will see examples of adding prefix and then add suffix to all the column names. We will also learn how to add prefix and suffix to select column names using rename_with() function.
library(tidyverse)
diamonds %>% head() # A tibble: 6 × 10 carat cut color clarity depth table price x y z <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
The syntax for using rename_with() function from dplyr is as follows.
rename_with(.data, .fn, .cols = everything(), ...)
dplyr’s rename_with() How to add prefix to all columns
Here is an example showing how to add prefix to all columns of a dataframe using dplyr’s rename_with(). The key idea is to write a function that takes each column value and adds the desired prefix. And we provide the function to rename_with()’s argument .fn.
In the example below we have written a function using paste0 function to add a prefix. Note the argument to the function is .x and it refers to the current column value.
diamonds %>% rename_with( .fn = function(.x){paste0("pre_", .x)}) %>% head()
Here is the result after adding “pre_” as. prefix to all the columns.
# A tibble: 6 × 10 pre_carat pre_cut pre_color pre_clarity pre_depth pre_table pre_price pre_x <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> 1 0.23 Ideal E SI2 61.5 55 326 3.95 2 0.21 Premium E SI1 59.8 61 326 3.89 3 0.23 Good E VS1 56.9 65 327 4.05 4 0.29 Premium I VS2 62.4 58 334 4.2 5 0.31 Good J SI2 63.3 58 335 4.34 6 0.24 Very Good J VVS2 62.8 57 336 3.94 # … with 2 more variables: pre_y <dbl>, pre_z <dbl>
dplyr’s rename_with(): How to add suffix to all columns
In this example we show how to add a suffix to all the columns of a dataframe using dplyr’s rename_with() function. Similar to adding a prefix, we need to provide a function that adds suffix to each columns as argument to rename_with() function using .fn argument.
diamonds %>% rename_with(.fn = function(.x){paste0(.x,"_post")}) %>% head()
After applying rename_with(), we get a dataframe with a specified suffix attached to all the columns names.
# A tibble: 6 × 10 carat_post cut_post color_post clarity_post depth_post table_post price_post <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> 1 0.23 Ideal E SI2 61.5 55 326 2 0.21 Premium E SI1 59.8 61 326 3 0.23 Good E VS1 56.9 65 327 4 0.29 Premium I VS2 62.4 58 334 5 0.31 Good J SI2 63.3 58 335 6 0.24 Very Good J VVS2 62.8 57 336 # … with 3 more variables: x_post <dbl>, y_post <dbl>, z_post <dbl>
dplyr’s rename_with(): How to add prefix to select columns
Often we might want to add prefix or suffix to select columns of a dataframe, not to all the column names. Here we have three examples showing how to add prefix to select columns using rename_with() function.
The main idea is to use .cols argument to rename_with() function and specify the columns we need to add prefix.
Example 1: Add prefix to select columns by specifying the column names
Here we add prefix to select columns by specifying the column names as a vector to .cols argument.
diamonds %>% rename_with( .fn = function(.x){paste0("diamond_", .x)}, .cols=c(carat, cut, color, clarity)) %>% head() # A tibble: 6 × 10 diamond_carat diamond_cut diamond_color diamond_clarity depth table price <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> 1 0.23 Ideal E SI2 61.5 55 326 2 0.21 Premium E SI1 59.8 61 326 3 0.23 Good E VS1 56.9 65 327 4 0.29 Premium I VS2 62.4 58 334 5 0.31 Good J SI2 63.3 58 335 6 0.24 Very Good J VVS2 62.8 57 336 # … with 3 more variables: x <dbl>, y <dbl>, z <dbl>
Example 2: Add prefix to select columns by using starts_with() function
Here we add prefix to select columns by using the tidy select function starts_with() as .cols’ argument.
diamonds %>% rename_with( .fn = function(.x){paste0("diamond_", .x)}, .cols=starts_with("c")) %>% head() # A tibble: 6 × 10 diamond_carat diamond_cut diamond_color diamond_clarity depth table price <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> 1 0.23 Ideal E SI2 61.5 55 326 2 0.21 Premium E SI1 59.8 61 326 3 0.23 Good E VS1 56.9 65 327 4 0.29 Premium I VS2 62.4 58 334 5 0.31 Good J SI2 63.3 58 335 6 0.24 Very Good J VVS2 62.8 57 336 # … with 3 more variables: x <dbl>, y <dbl>, z <dbl>
Example 3: Add prefix to select columns by using negation
In the third example of adding prefix to select columns by using negation and specifying the columns that we do not want to add prefix.
diamonds %>% rename_with( .fn = function(.x){paste0("diamond_", .x)}, .cols= -c(x,y,z)) %>% head() # A tibble: 6 × 10 diamond_carat diamond_cut diamond_color diamond_clarity diamond_depth <dbl> <ord> <ord> <ord> <dbl> 1 0.23 Ideal E SI2 61.5 2 0.21 Premium E SI1 59.8 3 0.23 Good E VS1 56.9 4 0.29 Premium I VS2 62.4 5 0.31 Good J SI2 63.3 6 0.24 Very Good J VVS2 62.8 # … with 5 more variables: diamond_table <dbl>, diamond_price <int>, x <dbl>, # y <dbl>, z <dbl>
dplyr’s rename_with(): How to add suffix to select columns
Here we see three examples showing how to add suffix to select columns using rename_with() function.
Again the key idea is to use .cols argument to rename_with() function and specify the columns we need to add suffix.
Example 1: Add suffix to select columns by specifying the names
diamonds %>% rename_with(.fn = function(.x){paste0(.x,"_diamond")}, .cols= c(carat,cut,color,clarity)) %>% head() # A tibble: 6 × 10 carat_diamond cut_diamond color_diamond clarity_diamond depth table price <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> 1 0.23 Ideal E SI2 61.5 55 326 2 0.21 Premium E SI1 59.8 61 326 3 0.23 Good E VS1 56.9 65 327 4 0.29 Premium I VS2 62.4 58 334 5 0.31 Good J SI2 63.3 58 335 6 0.24 Very Good J VVS2 62.8 57 336 # … with 3 more variables: x <dbl>, y <dbl>, z <dbl>
Example 2: Add suffix to select columns by using tidy select function
diamonds %>% rename_with( .fn = function(.x){paste0(.x, "_diamond")}, .cols=starts_with("c")) %>% head() # A tibble: 6 × 10 carat_diamond cut_diamond color_diamond clarity_diamond depth table price <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> 1 0.23 Ideal E SI2 61.5 55 326 2 0.21 Premium E SI1 59.8 61 326 3 0.23 Good E VS1 56.9 65 327 4 0.29 Premium I VS2 62.4 58 334 5 0.31 Good J SI2 63.3 58 335 6 0.24 Very Good J VVS2 62.8 57 336 # … with 3 more variables: x <dbl>, y <dbl>, z <dbl>
Example 3: Add suffix to select columns by negation
diamonds %>% rename_with(.fn = function(.x){paste0(.x,"_diamond")}, .cols= -c(x,y,z)) %>% head() # A tibble: 6 × 10 carat_diamond cut_diamond color_diamond clarity_diamond depth_diamond <dbl> <ord> <ord> <ord> <dbl> 1 0.23 Ideal E SI2 61.5 2 0.21 Premium E SI1 59.8 3 0.23 Good E VS1 56.9 4 0.29 Premium I VS2 62.4 5 0.31 Good J SI2 63.3 6 0.24 Very Good J VVS2 62.8 # … with 5 more variables: table_diamond <dbl>, price_diamond <int>, x <dbl>, # y <dbl>, z <dbl>
You might also want to check out deplyr’s rename() function for renaming individual columns using the syntax. rename(new_name=old_name)
1 comment
Comments are closed.