【问题标题】:Split a matrix based in col numbers in R根据 R 中的列号拆分矩阵
【发布时间】:2014-05-22 10:43:16
【问题描述】:

您好,我正在尝试拆分基于矩阵的列数。我的意思是,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`

【问题讨论】:

  • 您在寻找input[, 1]input[, 2]input[, k]吗?
  • 是的,循环应用

标签: r syntax split


【解决方案1】:

这是你想要的吗:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 2020-12-29
    相关资源
    最近更新 更多