Rename multiple columns given character vectors of column names and replacement
I don't know if this is the right way to approach it, but
library(dplyr)
df %>% rename_at(vars(col.from), function(x) col.to) %>% head(2)
# mpg cyl disp bar drat foo qsec baz am gear carb
# Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Also note that I live in the future:
# packageVersion("dplyr")
# # [1] ‘0.7.0’
Rename multiple columns by names
setnames
from the data.table
package will work on data.frame
s or data.table
s
library(data.table)
d <- data.frame(a=1:2,b=2:3,d=4:5)
setnames(d, old = c('a','d'), new = c('anew','dnew'))
d
# anew b dnew
# 1 1 2 4
# 2 2 3 5
Note that changes are made by reference, so no copying (even for data.frames!)
Renaming multiple columns from vector in dplyr chain
An option with rename_at
would be
df %>%
rename_at(vars(starts_with('old_name')), ~ new_names)
# A tibble: 1 x 6
# RID Var1 Var2 new_name1 new_name2 new_name3
# <dbl> <chr> <chr> <dbl> <dbl> <dbl>
#1 1.00 A B 4.00 8.00 20.0
But, it is possible to make a function that works with rename_if
by creating a logical index on the column names
df %>%
rename_if(grepl("^old_name", names(.)), ~ new_names)
# A tibble: 1 x 6
# RID Var1 Var2 new_name1 new_name2 new_name3
# <dbl> <chr> <chr> <dbl> <dbl> <dbl>
#1 1.00 A B 4.00 8.00 20.0
The rename_if
in general is checking at the values of the columns instead of the column names i.e.
new_names2 <- c('var1', 'var2')
df %>%
rename_if(is.character, ~ new_names2)
# A tibble: 1 x 6
# RID var1 var2 old_name1 old_name2 old_name3
# <dbl> <chr> <chr> <dbl> <dbl> <dbl>
#1 1.00 A B 4.00 8.00 20.0
Rename selected columns with a vector of strings
I managed to solve the problem thanks to @mt1022 comment. According to How to rename multiple columns given character vectors of column names and replacement in dplyr 0.6.0?:
First a vector with the new names have to be created.
names_boston = c(paste0("BOS_", sprintf("%02d", 1:31), "_time"))
Then the columns can be selected using grep
, and the new names can be feed to rename_at
.
df %>%
rename_at(vars(grep("Time", names(.))), ~names_boston)
And to avoid creating new vectors you can actually feed the vector to the previous line of code:
df %>%
rename_at(vars(grep("Time", names(.))), ~c(paste0("BOS_", sprintf("%02d", 1:31), "_time")))
Replace column names in dataframe based on a character vector that is ordered differently
We can use cols
to reorder the columns in our data frame:
df[,cols]
This basically uses the order of cols
to reorder our data frame. This can be seen as follows:
df[,c("B","A")]
df[,c("A","B")]
If we had a vector named cols
and set it as either c("A","B")
or c("B","A")
, we could then use this "trick" to help us reorder the columns.
Rename multiple columns with a vector
Instead of starts_with
, we could use matches
as starts_with
expects a single input, while matches
can have multiple values if we create a single string concatened with |
(OR
)
library(dplyr)
library(stringr)
df %>%
rename_at(vars(matches(str_c(ensembl, collapse="|"))),
~genes)
-output
# A tibble: 1 x 3
# sample ACTB GAPDH
# <chr> <dbl> <dbl>
1 sample_A 12 20
Or another option is to remove the .
followed by the characters in the column names that starts with 'Ensembl' and use a named vector in rename
df %>%
rename_at(vars(starts_with("Ensembl")), ~ str_remove(., "\\..*")) %>%
rename(!!! setNames(ensembl, genes))
# A tibble: 1 x 3
# sample ACTB GAPDH
# <chr> <dbl> <dbl>
#1 sample_A 12 20
How to rename columns by substituting suffix in R?
library(tidyverse)
eat10.18 %>%
rename_with(~str_replace(.,'_10p$', '_p_10'))
id eat_10 eat_p_10 run_p_10
1 1000 2 1 1
2 1001 4 2 1
3 1002 1 3 2
Related Topics
How to Add Multiple Captions in Ggplot2 Outside of the Main Graph Area
Calculate Correlation with Cor(), Only for Numerical Columns
How to Use Multiple Versions of the Same R Package
Knitr: How to Prevent Text Wrapping in Output
R - When Trying to Install Package: Internetopenurl Failed
Make Readline Wait for Input in R
Assign Value to Group Based on Condition in Column
Cbind 2 Dataframes with Different Number of Rows
Read/Write Data in Libsvm Format
How to Sort All Dataframes in a List of Dataframes on the Same Column
Combining 'Expression()' with '\N'
R Ifelse Avoiding Change in Date Format
Changing Factor Levels with Dplyr Mutate