[R] How to split a matrix into a few matrices?

Henrique Dallazuanna wwwhsd at gmail.com
Wed Jan 27 13:06:57 CET 2010


Try this:

a <- mvrnorm(100, m, c)

On Wed, Jan 27, 2010 at 9:50 AM, ayu2008 <ayuazwal at yahoo.com> wrote:
>
>
> Hi dear users,
>
> I try to split a matrix into a few matrices, for example, suppose that I
> have 1000X4 matrix from mvrnorm(1000,m,c) with
>
> m<-matrix(c(0,0,0,2),4,1)   and
> c<-matrix(c(1.0,0.2,-0.5,0.3,0.2,1,0.2,-0.5,-0.5,0.2,1,0.2,0.3,-0.5,0.2,1),4,4,byrow=T)
>
>
> How to split the matrix into
>
> a. 2 matrices which the first one consists of the first 500 rows, and the
> second one consist of the last 500 rows

split(as.data.frame(a), rep(1:2, each = 500))

>
> b. split the matrix equally and sequentially (according to the order of the
> rows) into say 10 matrices with 100 x 4 dimension

split(as.data.frame(a), rep(1:100, each = 10))

> c. how to do (b) randomly, not following the order of the rows, but without
> any overlapping. So all the 10 matrices will consists of different values
> from the original matrix.

split(as.data.frame(a)[sample(1:nrow(a)),], rep(1:10, each = 10))


> Your help is very much appreciated....thank you
>
>
> ayu
> --
> View this message in context: http://n4.nabble.com/How-to-split-a-matrix-into-a-few-matrices-tp1311535p1311535.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list