Is there a shorter version for the folowing principle to rename certain columns of a data frame?
data1<-data.frame(\"a\"=1:3,\"b\"=1:3,\"c\"=1:3)
data1Names&
The data.table
package has a setnames
function that will work on data.frames
library(data.table)
data1<-data.frame("a"=1:3,"b"=1:3,"c"=1:3)
#setnames(data1, "a", "hello")
#setnames(data1, "c", "world")
# or in one step
setnames(data1, c("a", "c"), c("hello", "world"))
data1
# hello b world
#1 1 1 1
#2 2 2 2
#3 3 3 3
All of the answers so far will make a copy of the data.frame
. setnames
has the added benefit that it changes the names by reference, without making a copy of the data.frame.