基于R中的col编号拆分矩阵

时间:2014-05-22 10:43:17

标签: r syntax split

您好我正在尝试拆分基于矩阵的列数。我的意思是,input是我想要的矩阵:

input[[1]] = column 1
input[[2]] = column 2
...
input[[k]] = column k

我试过了:

split(input, col(input1))

但我明白了:

input[[1]] = `All columns`
input[[2]] = `All columns`
...
input[[k]] = `All columns`

1 个答案:

答案 0 :(得分:3)

这是你想要的: -

mat <- matrix(rnorm(100),ncol=10,nrow=10)
colnames(mat) <- letters[1:10]

> mat[1:5,1:5]
           a          b          c          d           e
[1,]  0.04359815 -0.5465978  1.6571901  0.5318957 -0.37368263
[2,]  0.83665905  1.4243640 -1.6846726  0.8171491  0.81519568
[3,] -1.13277616 -1.0313740 -0.6788636  1.8289980 -1.06233673
[4,] -1.18810210 -0.2438800 -1.8984680 -0.9965713  0.04258266
[5,]  1.39090518  1.1525422  0.3488335  0.3175677  1.75836945

mat <- apply(mat,2,FUN=list)

mat[[1]]
[[1]]
[1]  0.04359815  0.83665905 -1.13277616 -1.18810210  1.39090518  0.50644256
[7] -0.93980249  0.99156864 -0.18153107 -0.82254772