janicebaratheon
janicebaratheon

Reputation: 999

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

Upvotes: 47

Views: 139198

Answers (7)

Priyansh
Priyansh

Reputation: 1248

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)

Upvotes: 2

Alex
Alex

Reputation: 15708

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.

Upvotes: 4

daknowles
daknowles

Reputation: 1430

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

Upvotes: 86

Marcel Hebing
Marcel Hebing

Reputation: 3182

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"

Upvotes: 6

johannes
johannes

Reputation: 14433

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")

Upvotes: 16

IRTFM
IRTFM

Reputation: 263362

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] )

Upvotes: 3

Roland
Roland

Reputation: 132706

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

Upvotes: 4

Related Questions