rename the columns name after cbind the data

janicebaratheon picture janicebaratheon · Jun 19, 2012 · Viewed 109.1k times · Source

enter image description here

merger <- cbind(as.character(Date),weather1$High,weather1$Low,weather1$Avg..High,weather1$Avg.Low,sale$Scanned.Movement[a])

After cbind the data, the new DF has column names automatically V1, V2...... I want rename the column by

colnames(merger)[,1] <- "Date"

but failed. And when I use merger$V1 ,

Error in merger$V1 : $ operator is invalid for atomic vectors

Answer

daknowles picture daknowles · Apr 2, 2014

You can also name columns directly in the cbind call, e.g.

cbind(date=c(0,1), high=c(2,3))

Output:

     date high
[1,]    0    2
[2,]    1    3