在R中将矩阵转换为数组

时间:2013-01-16 11:45:01

标签: arrays r list matrix

我有如下列表矩阵:

01_2_020.gth 02_2_020.gth 03_2_020.gth 04_2_015.gth 05_2_015.gth 06_2_015.gth 07_2_010.gth

 [1,] 0.5643314    0.5440568    0.5506353    0.4717183    0.4659001    0.4981198    0.3697551   
 [2,] 0.6365412    0.6135798    0.6211972    0.5207896    0.5208007    0.5519582    0.4210863   
 [3,] 0.6961687    0.6745536    0.6803473    0.5742673    0.5687056    0.596461     0.4613756   
 [4,] 0.7571837    0.7258654    0.7354272    0.6200432    0.6084758    0.6457854    0.5016579   

我想将此矩阵转换为具有以下结构的数组:

$`01_2_020.gth`

  [1] 1.3519346 1.3529605 1.3465736 1.3350906 1.3443445 1.3311501 1.3339374 1.3128332 1.3172605
 [10] 1.3070263 1.2991674 1.2943014 1.2892072 1.2857247 1.2764447 1.2644700 1.2608630 1.2597877
 [19] 1.2460450 1.2377009 1.2416192 1.2396816 1.2314797 1.2282107 1.2206790 1.2176491 1.2163807

$`02_2_020.gth`

  [1] 1.3259280 1.3180293 1.3153046 1.3101223 1.3014639 1.2927999 1.2851856 1.2813943 1.2719856
 [10] 1.2636101 1.2617434 1.2503571 1.2410282 1.2437357 1.2299740 1.2224429 1.2138308 1.2070487
 [19] 1.2131742 1.1957252 1.1943047 1.1841959 1.1792643 1.1695081 1.1530798 1.1613240 1.1542210

我真的不知道如何开始。

str函数在第一种情况下给出了我:

> str(x) 
 List of 570 
 $ : num 0.564
 $ : num 0.637
 $ : num 0.696
 $ : num 0.757
 $ : num 0.811
 $ : num 0.855
 $ : num 0.892
 $ : num 0.934
 $ : num 0.967
 $ : num 0.998
 $ : num 1.02
 $ : num 1.06
 $ : num 1.09
 $ : num 1.11
 $ : num 1.12
 $ : num 1.14
 $ : num 1.16
 $ : num 1.17
 $ : num 1.19
 $ : num 1.21
 $ : num 1.22
 $ : num 1.24
 $ : num 1.25
...
  [list output truncated]
 - attr(*, "dim")= int [1:2] 38 15
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:15] "01_2_020.gth" "02_2_020.gth" "03_2_020.gth" ...

str函数在第二种情况下给出了我的信息:

> str(groupedDissociationsValues)<br/>
List of 15<br/>
 $ 01_2_020.gth: num [1:120] 1.35 1.35 1.35 1.34 1.34 ...
 $ 02_2_020.gth: num [1:120] 1.33 1.32 1.32 1.31 1.3 ...
 $ 03_2_020.gth: num [1:120] 1.31 1.3 1.3 1.29 1.29 ...
 $ 04_2_015.gth: num [1:120] 1.18 1.18 1.18 1.17 1.16 ...
 $ 05_2_015.gth: num [1:120] 1.17 1.17 1.17 1.16 1.15 ...
 $ 06_2_015.gth: num [1:120] 1.18 1.18 1.17 1.18 1.17 ...
 $ 07_2_010.gth: num [1:120] 0.986 0.984 0.975 0.981 0.972 ...
 $ 08_2_010.gth: num [1:120] 0.955 0.957 0.952 0.947 0.941 ...
 $ 09_2_010.gth: num [1:120] 0.963 0.96 0.952 0.954 0.952 ...
 $ 10_2_004.gth: num [1:120] 0.558 0.558 0.554 0.547 0.551 ...
 $ 11_2_004.gth: num [1:120] 0.475 0.477 0.476 0.482 0.477 ...
 $ 12_2_004.gth: num [1:120] 0.529 0.523 0.521 0.523 0.515 ...
 $ 13_2_025.gth: num [1:120] 1.31 1.3 1.3 1.29 1.29 ...
 $ 14_2_025.gth: num [1:120] 1.33 1.33 1.33 1.32 1.31 ...
 $ 15_2_025.gth: num [1:120] 1.29 1.28 1.28 1.27 1.27 ...
 - attr(*, "dim")= int 15
 - attr(*, "dimnames")=List of 1
  ..$ : chr [1:15] "01_2_020.gth" "02_2_020.gth" "03_2_020.gth" ...

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

如果您取消列表,它会将其按顺序存储为矢量。然后只是强制到数组(仍然是一个向量,但具有维度属性)。

> test <- rep(list(1),99)
> testArray <- array( unlist(test), dim=c(3,3,11) )
> testArray
, , 1

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1

, , 2

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1
...