I've been using this function to paste columns of a data frame
pasteDFcol <- function(mydf,clmnnames=c("V1","V2"),sepChar=" "){
do.call("paste",c(mydf[clmnnames],sep=sepChar))
}
You can pass your matrix using as.data.frame, and specify which columns you want to join, e.g.
a=matrix(1:600000,ncol=600)
a1.df <- data.frame(V1=pasteDFcol(as.data.frame(a),clmnnames=paste0("V",1:300)),
V2=pasteDFcol(as.data.frame(a),clmnnames=paste0("V",301:600)))
a2 <- as.matrix(a1.df)
It is about twice as fast as the method you were using.
For pasting columns 1:4, 5:8... or any other rolling frame, lets modify the function to take starting column, and number of columns to be pasted as arguments, then use sapply
.
pasteDFcol <- function(clmStart, clmNum=4, mydf, sepChar=" "){
do.call("paste",c(mydf[paste0("V",clmStart:(clmStart+clmNum-1))],sep=sepChar))
}
a=matrix(1:400,ncol=40)
pasteDFcol(clmStart=1, clmNum=4,mydf=as.data.frame(a))
a1 <- sapply(seq(1, 40, by=4), pasteDFcol, clmNum=4, mydf=as.data.frame(a))