[R] Create matrix with subset from unlist

Muhammad Rahiz muhammad.rahiz at ouce.ox.ac.uk
Fri Jan 29 15:30:08 CET 2010


Hello all,

I'm trying to create a 2x2 matrix, 32 times after unlist() so that I can 
convert the list to matrix. I've looked through the R archive but 
couldn't find the answer. There is what I've done.


 > f <- system("ls *.txt", intern=TRUE)
 > x <- lapply(f, read.table)
 > x
[[1]]
     V1    V2
1 -27.3  14.4
2  29.0 -38.1

[[2]]
     V1   V2
1  14.4 29.0
2 -38.1 -3.4

[[3]]
    V1    V2
1 29.0 -38.1
2 -3.4  55.1

[[4]]
     V1   V2
1 -38.1 -3.4
2  55.1 -1.0

[[5]]
    V1   V2
1 -3.4 55.1
2 -1.0 21.9

[[6]]
    V1    V2
1 55.1  -1.0
2 21.9 -10.9

...

 > xx <- unlist(x)
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-27.3  29.0  14.4 -38.1  14.4 -38.1  29.0  -3.4  29.0  -3.4 -38.1  55.1
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-38.1  55.1  -3.4  -1.0  -3.4  -1.0  55.1  21.9  55.1  21.9  -1.0 -10.9
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -1.0 -10.9  21.9  -7.8  21.9  -7.8 -10.9 -48.2 -10.9 -48.2  -7.8 -44.9
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -7.8 -44.9 -48.2 -43.8 -48.2 -43.8 -44.9 -10.3 -44.9 -10.3 -43.8  44.2
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-43.8  44.2 -10.3  -0.5 -10.3  -0.5  44.2  96.7  44.2  96.7  -0.5 -32.0
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -0.5 -32.0  96.7  -0.2  96.7  -0.2 -32.0 -38.6 -32.0 -38.6  -0.2  73.6
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
 -0.2  73.6 -38.6 -17.5 -38.6 -17.5  73.6 -57.8  73.6 -57.8 -17.5  10.7
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-17.5  10.7 -57.8 -33.4 -57.8 -33.4  10.7  46.1  10.7  46.1 -33.4  26.7
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-33.4  26.7  46.1 -37.3  46.1 -37.3  26.7   1.2  26.7   1.2 -37.3  36.3
  V11   V12   V21   V22   V11   V12   V21   V22   V11   V12   V21   V22
-37.3  36.3   1.2  39.6   1.2  39.6  36.3  31.0  36.3 -27.3  39.6  14.4
  V11   V12   V21   V22   V11   V12   V21   V22
 39.6  29.0  31.0 -38.1  31.0  -3.4 -27.3  55.1


The output should be

[[1]]
          [,1]      [,2]
[1,]    -27.3    14.4
[2,]     29.0    -38.1

[[2]]
           [,1]     [,2]
[1,]      14.4    29.0
[2,]     -38.1    -3.4

[[3]]
          [,1]     [,2]
[1,]    29.0    -38.1
[2,]    -3.4     55.1

...
Thanks and much appreciated!



Muhammad



More information about the R-help mailing list