R:使用列表的seq_along向量进行循环迭代

时间:2015-01-08 19:49:44

标签: r loops for-loop

考虑以下代码:

listLength <- sample(1:10, 1)
vectorLength <- sample(1:10, 1)

x <- vector("list")

for(i in 1:listLength){

  x[[i]] <- sample(1:10, vectorLength)
}


transform <- function(x){

  result <- vector("list")

  for(i in seq_along(x[[1]])){ 

    temp <- vector("numeric")

    for(j in seq_along(x)){

      temp <- c(temp, x[[j]][i])

    }

    result[[i]] <- temp

  }

  return(result)


}

最后

y <- transform(x)

x是随机长度的列表,x的向量元素具有随机但总是相等的长度。

transform函数返回一个新的列表y,它是vectorLength的列表,其中包含listLength的向量。

问题:

for(i in seq_along(x[[1]]))

这是常见的方法吗?只需获取for循环中第一个元素的长度?或者是否存在某种sublength函数,它给出了每个子元素的长度。

当然我可以使用vectorLength变量,但可以把它想象成一个只产生x变量的复杂算法。

我觉得这是糟糕的编码...

1 个答案:

答案 0 :(得分:0)

我可能会使用mapply在函数中运行那些“花哨的东西”。一个惊喜可能是它返回其矩阵结果的列主要顺序而不是你可能预期的结果(通过用t(.)

包装它很容易解决
mapply(function(x,y) c(x,y,seq(y,y+(y-x),length=8)), x,x2)
          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
 [1,]  1.00000  2.00000  3.00000  4.00000  5.00000  6.00000
 [2,] 11.00000 12.00000 13.00000 14.00000 15.00000 16.00000
 [3,] 11.00000 12.00000 13.00000 14.00000 15.00000 16.00000
 [4,] 12.42857 13.42857 14.42857 15.42857 16.42857 17.42857
 [5,] 13.85714 14.85714 15.85714 16.85714 17.85714 18.85714
 [6,] 15.28571 16.28571 17.28571 18.28571 19.28571 20.28571
 [7,] 16.71429 17.71429 18.71429 19.71429 20.71429 21.71429
 [8,] 18.14286 19.14286 20.14286 21.14286 22.14286 23.14286
 [9,] 19.57143 20.57143 21.57143 22.57143 23.57143 24.57143
[10,] 21.00000 22.00000 23.00000 24.00000 25.00000 26.00000
          [,7]     [,8]     [,9]    [,10]
 [1,]  7.00000  8.00000  9.00000 10.00000
 [2,] 17.00000 18.00000 19.00000 20.00000
 [3,] 17.00000 18.00000 19.00000 20.00000
 [4,] 18.42857 19.42857 20.42857 21.42857
 [5,] 19.85714 20.85714 21.85714 22.85714
 [6,] 21.28571 22.28571 23.28571 24.28571
 [7,] 22.71429 23.71429 24.71429 25.71429
 [8,] 24.14286 25.14286 26.14286 27.14286
 [9,] 25.57143 26.57143 27.57143 28.57143
[10,] 27.00000 28.00000 29.00000 30.00000

转置输出:

> t(mapply(function(x,y) c(x,y,seq(y,y+(y-x),length=8)), x,x2))
      [,1] [,2] [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
 [1,]    1   11   11 12.42857 13.85714 15.28571 16.71429 18.14286
 [2,]    2   12   12 13.42857 14.85714 16.28571 17.71429 19.14286
 [3,]    3   13   13 14.42857 15.85714 17.28571 18.71429 20.14286
 [4,]    4   14   14 15.42857 16.85714 18.28571 19.71429 21.14286
 [5,]    5   15   15 16.42857 17.85714 19.28571 20.71429 22.14286
 [6,]    6   16   16 17.42857 18.85714 20.28571 21.71429 23.14286
 [7,]    7   17   17 18.42857 19.85714 21.28571 22.71429 24.14286
 [8,]    8   18   18 19.42857 20.85714 22.28571 23.71429 25.14286
 [9,]    9   19   19 20.42857 21.85714 23.28571 24.71429 26.14286
[10,]   10   20   20 21.42857 22.85714 24.28571 25.71429 27.14286
          [,9] [,10]
 [1,] 19.57143    21
 [2,] 20.57143    22
 [3,] 21.57143    23
 [4,] 22.57143    24
 [5,] 23.57143    25
 [6,] 24.57143    26
 [7,] 25.57143    27
 [8,] 26.57143    28
 [9,] 27.57143    29
[10,] 28.57143    30