[r] rename the columns name after cbind the data

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

This question is related to r rename

The answer is


Try:

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

Example

Here is a simple example:

a <- 1:10
b <- cbind(a, a, a)
colnames(b)

# change the first one
colnames(b)[1] <- "abc"

# change all colnames
colnames(b) <- c("aa", "bb", "cc")

you gave the following example in your question:

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

the problem is the comma: colnames() returns a vector, not a matrix, so the solution is:

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

If you pass only vectors to cbind() it creates a matrix, not a dataframe. Read ?data.frame.


A way of producing a data.frame and being able to do this in one line is to coerce all matrices/data frames passed to cbind into a data.frame while setting the column names attribute using setNames:

a = matrix(rnorm(10), ncol = 2)
b = matrix(runif(10), ncol = 2)

cbind(setNames(data.frame(a), c('n1', 'n2')), 
      setNames(data.frame(b), c('u1', 'u2')))

which produces:

          n1        n2         u1        u2
1 -0.2731750 0.5030773 0.01538194 0.3775269
2  0.5177542 0.6550924 0.04871646 0.4683186
3 -1.1419802 1.0896945 0.57212043 0.9317578
4  0.6965895 1.6973815 0.36124709 0.2882133
5  0.9062591 1.0625280 0.28034347 0.7517128

Unfortunately, there is no setColNames function analogous to setNames for data frames that returns the matrix after the column names, however, there is nothing to stop you from adapting the code of setNames to produce one:

setColNames <- function (object = nm, nm) {
    colnames(object) <- nm
    object
}

See this answer, the magrittr package contains functions for this.


If you offer cbind a set of arguments all of whom are vectors, you will get not a dataframe, but rather a matrix, in this case an all character matrix. They have different features. You can get a dataframe if some of your arguments remain dataframes, Try:

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

It's easy just add the name which you want to use in quotes before adding vector

a_matrix <- cbind(b_matrix,'Name-Change'= c_vector)