简体   繁体   English

将列放在带有R的列表中

[英]Drop column in a list with R

Sample data: 样本数据:

list.data <- list(matrix(seq(1,30,2),5,3),matrix(seq(1,10,2),5,2))

I would like to drop first column of the matrix in the list, but keep the proceeding columns vertically. 我想将矩阵的第一列放在列表中,但将后续的列保持垂直。 (as [1:n,1] matrix) (作为[1:n,1]矩阵)

This is what I have tried (and otherwise): 这是我尝试过的(否则):

lapply(list.data, function(x) x[,2:ncol(x)])

but the initial 2 column matrix keeps flipping into horizontal position. 但是最初的2列矩阵一直翻转到水平位置。

What I would like to have is this: 我想拥有的是:

    [[1]]
    [,1] [,2]
[1,]   11   21
[2,]   13   23
[3,]   15   25
[4,]   17   27
[5,]   19   29

    [[2]]
    [,1]
 [1] 1 
 [2] 3 
 [3] 5 
 [4] 7 
 [5] 9

Just add drop = FALSE to your command to keep the dimension attribute intact. 只需在命令中添加drop = FALSE即可保持维度属性完整。

> lapply(list.data, function(x) x[,2:ncol(x), drop = FALSE])
[[1]]
     [,1] [,2]
[1,]   11   21
[2,]   13   23
[3,]   15   25
[4,]   17   27
[5,]   19   29

[[2]]
     [,1]
[1,]    1
[2,]    3
[3,]    5
[4,]    7
[5,]    9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM