我正在使用sapply()
生成30个随机漫步模拟,其中包含我自己的levy.walk.flights()
函数。
simmulations=30
granMAT <- sapply(1:simmulations, function(i){
a <- levy.walk.flights()[[1]]
cbind(a,rep(i, length(a[,1])))
})
对象a
收到一个data.frame
,其中有两列:x,y;加上下一行代码中包含的ID列。
此代码完美运行并产生所有30个模拟。唯一的问题是sapply()
返回30 * 3(90)数组的列表。请参阅str(granMAT)
示例,其中包含3个模拟。
List of 9
$ : num [1:5748] 0 0.00357 0.0074 0.01071 0.01418 ...
$ : num [1:5748] 0 0.135 0.28 0.406 0.537 ...
$ : int [1:5748] 1 1 1 1 1 1 1 1 1 1 ...
$ : num [1:2434] 0 -0.131 -0.261 -0.385 -0.527 ...
$ : num [1:2434] 0 -0.048 -0.0955 -0.1412 -0.1933 ...
$ : int [1:2434] 2 2 2 2 2 2 2 2 2 2 ...
$ : num [1:2301] 0 -0.0887 -0.1829 -0.276 -0.364 ...
$ : num [1:2301] 0 0.0941 0.1939 0.2927 0.386 ...
$ : int [1:2301] 3 3 3 3 3 3 3 3 3 3 ...
- attr(*, "dim")= int [1:2] 3 3
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "x" "y" "rep(i, length(a[, 1]))"
..$ : NULL`
我希望sapply()
返回包含3列的data.frame
:x,y,ID。并且所有数据都在这3列之下。我尝试了do.call()
和几个library(plyr)
函数,但我无法确定哪些函数可以让我这样做。
感谢您的帮助!
答案 0 :(得分:0)
simmulations=30
granMAT <- sapply(1:simmulations, function(i){
a <- levy.walk.flights()[[1]]
cbind(a,rep(i, length(a[,1])))
}) %>% rbind_all
rbind_all
是dplyr等于do.call(“rbind”,list)的简写函数。将结果从sapply导入rbind_all,意味着它将获取列表并将所有行绑定在一起,返回data.frame。