I have the following tibble:
library(tidyverse) df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4)), .Names = c("Sepal.Length", "Sepal.Width", "Petal.Length"), row.names = c(NA, 5L), class = c("tbl_df", "tbl", "data.frame"))
That looks like this:
> df # A tibble: 5 × 3 Sepal.Length Sepal.Width Petal.Length * <dbl> <dbl> <dbl> 1 5.1 3.5 1.4 2 4.9 3.0 1.4 3 4.7 3.2 1.3 4 4.6 3.1 1.5 5 5.0 3.6 1.4
What I want to do is to replace Sepal.Length
and Petal.Length
with appended string to_app <- ".xxx"
resulting in:
Sepal.Length.xxx Sepal.Width Petal.Length.xxx 5.1 3.5 1.4 4.9 3.0 1.4 4.7 3.2 1.3 4.6 3.1 1.5 5.0 3.6 1.4
I tried this with error:
df %>% rename(paste(Sepal.Length,to_app,sep="") = Petal.Length,paste(Sepal.Width,to_app,sep="") = Petal.Length)
Error: unexpected '=' in "df %>% rename(paste(Sepal.Length,to_app,sep="") ="
To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.
rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.
You can rename a column in R in many ways. For example, if you want to rename the column called “A” to “B” you can use this code: names(dataframe)[names(dataframe)==”A”] <- “B”. This way, you changed the column name to “B”.
How to change column headers of a data-frame in R? colnames () function can be used to change the column names of a data-frame column in R. colnames () function can be used for changing one column name at a time, also all the column names can be changed at once.
You can use the rename_at()
function for this (starting in dplyr_0.7.0) or rename_with()
(starting in dplyr_1.0.0).
For example, you can pass the variables you want to rename as strings. In your example, the paste0
function can be used to append on the appropriate suffix to each column.
cols = c("Sepal.Length", "Petal.Length") to_app = ".xxx"
For dplyr_1.0.0, use rename_with()
. The function argument comes before the column argument. You can use the tidy-select function all_of()
(or others) to choose columns.
rename_with(df, .fn = ~paste0(., to_app), .cols = all_of(cols) ) # A tibble: 5 x 3 Sepal.Length.xxx Sepal.Width Petal.Length.xxx * <dbl> <dbl> <dbl> 1 5.1 3.5 1.4 2 4.9 3 1.4 3 4.7 3.2 1.3 4 4.6 3.1 1.5 5 5 3.6 1.4
Earlier versions of dplyr use rename_at()
. This has been superseded in version 1.0.0, which means there are new functions to use as above but this particular function is not going away.
rename_at(df, cols, list( ~paste0(., to_app) ) ) # A tibble: 5 x 3 Sepal.Length.xxx Sepal.Width Petal.Length.xxx * <dbl> <dbl> <dbl> 1 5.1 3.5 1.4 2 4.9 3.0 1.4 3 4.7 3.2 1.3 4 4.6 3.1 1.5 5 5.0 3.6 1.4
You can also use the select helper functions to choose variables for renaming, such as contains
.
rename_at(df, vars( contains("Length") ), list( ~paste0(., ".xxx") ) ) # A tibble: 5 x 3 Sepal.Length.xxx Sepal.Width Petal.Length.xxx * <dbl> <dbl> <dbl> 1 5.1 3.5 1.4 2 4.9 3.0 1.4 3 4.7 3.2 1.3 4 4.6 3.1 1.5 5 5.0 3.6 1.4
This list()
coding replaces the previous funs()
coding starting in dplyr_0.7.0. Previously, e.g., funs( paste0(., to_app) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With