问题
How can I concatenate matrices of same columns but different number of rows? For example, I
want to concatenate a ( dim(a) = 15 7000 ) and b (dim(b) = 16 7000) and I want the result to be a matrix of 31 rows by 7000 columns. Can I do this for matrices of different rows and columns.? Say I want to combine a matrix of 15 rows and 7000 columns with another of 16 rows and 7500 columns. Can I create one dataset with that?
回答1:
Sounds like you're looking for rbind:
> a<-matrix(nrow=10,ncol=5)
> b<-matrix(nrow=20,ncol=5)
> dim(rbind(a,b))
[1] 30 5
Similarly, cbind stacks the matrices horizontally.
I am not entirely sure what you mean by the last question ("Can I do this for matrices of different rows and columns.?")
回答2:
cbindX from the package gdata combines multiple columns of differing column and row lengths. Check out the page here:
http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/gdata/html/cbindX.html
It takes multiple comma separated matrices and data.frames as input :) You just need to
install.packages("gdata", dependencies=TRUE)
and then
library(gdata)
concat_data <- cbindX(df1, df2, df3) # or cbindX(matrix1, matrix2, matrix3, matrix4)
来源:https://stackoverflow.com/questions/7324833/concatenating-matrices-in-r