Manuel Weinkauf
Manuel Weinkauf

Reputation: 350

Combine several columns under same name

I am trying to get the mvr function in the R-package pls to work. When having a look at the example dataset yarn I realized that all 268 NIR columns are in fact treated as one column:

 library(pls)
 data(yarn)
 head(yarn)
 colnames(yarn)

I would need that to use the function with my data (so that a multivariate datset is treated as one entity) but I have no idea how to achive that. I tried

 TT<-matrix(NA, 2, 3)
 colnames(TT)<-rep("NIR", ncol(TT))
 TT
 colnames(TT)

You will notice that while all columns have the same heading, colnames(TT) shows a vector of length three, because each column is treated separately. What I would need is what can be found in yarn, that the colname "NIR" occurs only once and applies columns 1-268 alike.

Does anybody know how to do that?

Upvotes: 0

Views: 100

Answers (1)

user20650
user20650

Reputation: 25914

You can just assign the matrix to a column of a data.frame

TT <- matrix(1:6, 2, 3 )

# assign to an existing dataframe
out <- data.frame(desnity = 1:nrow(TT))
out$NIR <- TT
str(out)

# assign to empty dataframe
out <- data.frame(matrix(integer(0), nrow=nrow(TT))) ; 
out$NIR <- TT

Upvotes: 1

Related Questions