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
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.
The cbind function is used to combine vectors, matrices and/or data frames by columns.
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) .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With