Skip to content

Merge matrices with different column names

3 messages · Charles Determan Jr, Jeff Newmiller, Rui Barradas

#
If they have the same number of rows, you can use cbind() to create one object to write out.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Charles Determan Jr <deter088 at umn.edu> wrote:

            
#
Hello,

If you just want to write the matrices to a csv file instead of 
write.csv you can use write.table with options append=TRUE, sep="," and 
col.names=FALSE.

If you want to merge (rbind) them, you can use code similar to


pattern1 <- "(^var)[[:digit:]]+(.*$)"
pattern2 <- "(^var)_n_(.*$)"

colnames(Matrix1) <- sub(pattern1, "\\1_n_\\2", colnames(Matrix1))
colnames(Matrix2) <- sub(pattern1, "\\1_n_\\2", colnames(Matrix2))

Mat <- rbind(Matrix1, Matrix2)
colnames(Mat) <- sub(pattern2, "\\1_\\2", colnames(Mat))
Mat

Hope this helps,

Rui Barradas
Em 26-10-2012 04:51, Charles Determan Jr escreveu: