【问题标题】:How to create a series of list elements using for loop?如何使用 for 循环创建一系列列表元素?
【发布时间】:2019-08-24 14:39:59
【问题描述】:

我有一些包含多个时间序列的大型数据框,我试图在其中创建单个时间序列的列表。数据的结构没有很好的标识符,所以沿着拆分路线走似乎不正确,为什么我要尝试子集路线。

问题可以这样证明:

set.seed(1)
test1 <- matrix(rnorm(90), nrow = 10, ncol = 9)
testobj <- list()
indexlist <- list(1:3, 4:6, 7:9)

for(i in indexlist) {
    for(j in (1:3)) {
        testobj[[j]] <- test1[, i]
    }
}

结果是我的列表 testobj 是一个包含 test1[, 7:9] 的最后一个子集元素的 3 个元素的列表,而不是

testobj[[1]] <- test1[, 1:3]
testobj[[2]] <- test1[, 4:6]
testobj[[3]] <- test1[, 7:9]

这是我真正追求的。当我在控制台中运行for(i in indexlist) {print(i)} 时,它似乎生成了正确的 3 子集索引,因此,如果有人能帮助解释其出错的原因并提出更正建议,我们将不胜感激。

【问题讨论】:

    标签: r list loops


    【解决方案1】:

    如果你想使用for 循环,你只需要一个循环来循环indexlist

    for( i in seq_along(indexlist)) { 
         testobj[[i]] <- test1[,indexlist[[i]]] 
    }
    
    #[[1]]
    #            [,1]        [,2]        [,3]
    # [1,] -0.6264538  1.51178117  0.91897737
    # [2,]  0.1836433  0.38984324  0.78213630
    # [3,] -0.8356286 -0.62124058  0.07456498
    # [4,]  1.5952808 -2.21469989 -1.98935170
    # [5,]  0.3295078  1.12493092  0.61982575
    # [6,] -0.8204684 -0.04493361 -0.05612874
    # [7,]  0.4874291 -0.01619026 -0.15579551
    # [8,]  0.7383247  0.94383621 -1.47075238
    # [9,]  0.5757814  0.82122120 -0.47815006
    #[10,] -0.3053884  0.59390132  0.41794156
    
    #[[2]]
    #             [,1]       [,2]       [,3]
    # [1,]  1.35867955 -0.1645236  0.3981059
    #...
    

    不过,你也可以使用lapply

    lapply(indexlist, function(x) test1[, x])
    

    【讨论】:

      【解决方案2】:

      我们可以使用sapply 和simplify = FALSE

      sapply(indexlist, function(x) test1[, x] , simplify = FALSE)
      #[[1]]
      #            [,1]        [,2]        [,3]
      # [1,] -0.6264538  1.51178117  0.91897737
      # [2,]  0.1836433  0.38984324  0.78213630
      # [3,] -0.8356286 -0.62124058  0.07456498
      # [4,]  1.5952808 -2.21469989 -1.98935170
      # [5,]  0.3295078  1.12493092  0.61982575
      # [6,] -0.8204684 -0.04493361 -0.05612874
      # [7,]  0.4874291 -0.01619026 -0.15579551
      # [8,]  0.7383247  0.94383621 -1.47075238
      # [9,]  0.5757814  0.82122120 -0.47815006
      #[10,] -0.3053884  0.59390132  0.41794156
      
      #[[2]]
      #             [,1]       [,2]       [,3]
      # [1,]  1.35867955 -0.1645236  0.3981059
      # [2,] -0.10278773 -0.2533617 -0.6120264
      # [3,]  0.38767161  0.6969634  0.3411197
      # [4,] -0.05380504  0.5566632 -1.1293631
      # [5,] -1.37705956 -0.6887557  1.4330237
      # [6,] -0.41499456 -0.7074952  1.9803999
      # [7,] -0.39428995  0.3645820 -0.3672215
      # [8,] -0.05931340  0.7685329 -1.0441346
      # [9,]  1.10002537 -0.1123462  0.5697196
      #[10,]  0.76317575  0.8811077 -0.1350546
      
      #[[3]]
      #             [,1]         [,2]       [,3]
      # [1,]  2.40161776  0.475509529 -0.5686687
      # [2,] -0.03924000 -0.709946431 -0.1351786
      # [3,]  0.68973936  0.610726353  1.1780870
      # [4,]  0.02800216 -0.934097632 -1.5235668
      # [5,] -0.74327321 -1.253633400  0.5939462
      # [6,]  0.18879230  0.291446236  0.3329504
      # [7,] -1.80495863 -0.443291873  1.0630998
      # [8,]  1.46555486  0.001105352 -0.3041839
      # [9,]  0.15325334  0.074341324  0.3700188
      #[10,]  2.17261167 -0.589520946  0.2670988
      

      或与map 来自purrr

      library(purrr)
      map(indexlist, ~ test1[, .x])
      

      【讨论】:

        猜你喜欢
        • 2013-03-17
        • 1970-01-01
        • 2019-12-10
        • 1970-01-01
        • 2017-03-24
        • 1970-01-01
        • 2013-09-09
        • 2020-11-14
        • 1970-01-01
        相关资源
        最近更新 更多