[R] Splitting matrix into several small matrices

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Jul 28 08:13:01 CEST 2009


I take it you want to split the matrix into sub-matrices for which some collection of columns is constant.  In your case this is the last four columns.

Here's an idea:

> z <- matrix(c(13,1,1,1,1,12,0,0,0,0,8,1,0,1,1,8,0,1,0,0,
+                   10,1,1,1,1,3,0,1,0,0,3,1,0,1,1,6,1,1,1,1),8,5,byrow = T)
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]   13    1    1    1    1
[2,]   12    0    0    0    0
[3,]    8    1    0    1    1
[4,]    8    0    1    0    0
[5,]   10    1    1    1    1
[6,]    3    0    1    0    0
[7,]    3    1    0    1    1
[8,]    6    1    1    1    1

> ind <- do.call(paste, data.frame(z[, 2:5]))

> split(data.frame(z), ind)
$`0 0 0 0`
  X1 X2 X3 X4 X5
2 12  0  0  0  0

$`0 1 0 0`
  X1 X2 X3 X4 X5
4  8  0  1  0  0
6  3  0  1  0  0

$`1 0 1 1`
  X1 X2 X3 X4 X5
3  8  1  0  1  1
7  3  1  0  1  1

$`1 1 1 1`
  X1 X2 X3 X4 X5
1 13  1  1  1  1
5 10  1  1  1  1
8  6  1  1  1  1

>  


Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of kathie
Sent: Tuesday, 28 July 2009 8:47 AM
To: r-help at r-project.org
Subject: [R] Splitting matrix into several small matrices


Dear R users...

I need to split this matrix(or dataframe), for example,

z <- matrix(c(13,1,1,1,1,12,0,0,0,0,8,1,0,1,1,8,0,1,0,0,
                  10,1,1,1,1,3,0,1,0,0,3,1,0,1,1,6,1,1,1,1),8,5,byrow = T)


> z
     [,1] [,2] [,3] [,4] [,5]
[1,]   13    1    1    1    1
[2,]   12    0    0    0    0
[3,]    8    1    0    1    1
[4,]    9    0    1    0    0
[5,]   10    1    1    1    1
[6,]    3    0    1    0    0
[7,]    3    1    0    1    1
[8,]    6    1    1    1    1


(actually, z matrix is big, about 1000*15 matrix)

to 4 matrices like this way,


#- 1st matrix--------------------------------------
13    1    1    1    1
10    1    1    1    1
 6    1    1    1    1

#- 2nd matrix--------------------------------------
12    0    0    0    0


#- 3rd matrix--------------------------------------
8    1    0    1    1
3    1    0    1    1


#- 4th matrix--------------------------------------
9    0    1    0    0
3    0    1    0    0



Any comments will be greatly appreciated.

Kathryn Lord
-- 
View this message in context: http://www.nabble.com/Splitting-matrix-into-several-small-matrices-tp24689585p24689585.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.




More information about the R-help mailing list