通过2列将数据子集化为数据帧的有效方法

时间:2016-03-07 18:00:01

标签: r

我的数据如下所示:

> dput(mylist)
    list(structure(c(60.0090409674794, 60.0100409681621, 60.0110409688448, 
    60.0120409695275, 60.0130409702102, 60.0140409708929, 60.0150409715756, 
    60.0160409722583, 60.0170409729409, 60.0180409736236, -0.055, 
    -0.047, -0.034, -0.021, -0.015, -0.013, -0.01, -0.005, -0.003, 
    -0.007, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 6, 6, 6, 6, 6, 
    6, 6, 6, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(NULL, c("", 
    "", "", "i"))), structure(c(257.906176069569, 257.907176070251, 
    257.908176070934, 257.909176071617, 257.910176072299, 257.911176072982, 
    257.912176073665, 257.913176074348, 257.91417607503, 257.915176075713, 
    -0.001, -0.005, -0.013, -0.013, -0.002, 0.012, 0.016, 0.01, 0.004, 
    0.004, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 6, 6, 6, 6, 6, 
    6, 6, 6, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(NULL, c("", 
    "", "", "i"))), structure(c(1057.51772195514, 1057.51872195582, 
    1057.5197219565, 1057.52072195719, 1057.52172195787, 1057.52272195855, 
    1057.52372195924, 1057.52472195992, 1057.5257219606, 1057.52672196128, 
    0.035, 0.03, 0.026, 0.023, 0.013, -0.012, -0.049, -0.079, -0.078, 
    -0.039, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 6, 
    6, 6, 6, 6, 6, 6, 6, 6, 6), .Dim = c(10L, 4L), .Dimnames = list(
        NULL, c("", "", "", "i"))))

我的目标是在几个参数上对这些数据进行子集化:每个列中的频率号为3列,第4列为id号。因此,如果我有6个唯一的ID号和6个唯一的频率,我最终得到36个独特的data.frames。最直接的方法是使用for循环和if语句如下

for (i in seq_along(mylist){
       if (mylist[[i]][[,3]][1]== 10 && (mylist[[i]][[,4]][1]== 1)
           testdata=cbind(testdata,trials[[i]][,2]

        ...etc

}

但这对于这项任务来说很费力,需要很多时间。

注意,我只需要第二列而不是列表中的整个data.frame。我的猜测是来自filter的{​​{1}}可能是最好的选择,但不确定如何为36个独特的帧实现它。另外,我听说过bitmasks可能是另一个好方法。

0 个答案:

没有答案