How can I rename all columns of a data frame based on another data frame in R?
A B C D
1 2 3 4
DataFrame1 <- read.table(con <- file("clipboard"), header=T)
Col1 Col2
A E
B Q
C R
D Z
DataFrame2 <- read.table(con <- file("clipboard"), header=T)
colnames(DataFrame1) <- DataFrame2$Col2
If the column names didn't go in order like they do in the example you'd have to use match
:
DataFrame2$Col2[match(names(DataFrame1),DataFrame2$Col1)]