Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rename the columns name after cbind the data

Tags:

r

rename

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 
like image 315
janicebaratheon Avatar asked Jun 19 '12 18:06

janicebaratheon


People also ask

How do you change the name of a column in a dataset?

To rename columns, we can pass a dictionary to the columns argument. The keys are the columns you want to change and the values are the new names for these columns. We can also set the argument inplace to True for the change to happen in the existing DataFrame.

What is the use of Cbind () function?

The cbind function is used to combine vectors, matrices and/or data frames by columns.

How do I rename a column in a dataset in R?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B”, again, you can run the following code: rename(dataframe, B = A) .


1 Answers

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 
like image 69
daknowles Avatar answered Sep 19 '22 06:09

daknowles