我正在使用lapply和mapply来创建一堆操作。我的数据由列表组成。
对于其中一项计算,我遇到了一些关于如何使用任何应用函数的问题。这是我想要做的一个例子:
#list with a collection of objects, each object has a vector with the their 2 closest neighbors identification.
list_a<-list(c(2,4),c(1,3),c(1,4),c(3,2))
#list with the obs of each point
list_obs<-list(runif(1000,1,2),runif(1000,0,2),runif(1000,0.5,2),runif(1000,0.1,1.5))
list_a中的位置n
对应于邻居。要点n
位置n
对应于点n
我想要做的是创建一个新列表,该列表将在每个位置n
存储neighb的观察列表。分:
#output
out=list(list(list_obs[[2]],list_obs[[4]]),list(list_obs[[1]],list_obs[[3]]),list(list_obs[[1]],list_obs[[4]]),list(list_obs[[3]],list_obs[[2]]))
> str(out)
List of 4
$ :List of 2
..$ : num [1:1000] 1.673 1.423 0.228 1.758 1.65 ...
..$ : num [1:1000] 0.679 1.341 0.148 0.867 0.724 ...
$ :List of 2
..$ : num [1:1000] 1.25 1.5 1.58 1.54 1.6 ...
..$ : num [1:1000] 0.526 1.545 1.848 0.711 0.697 ...
$ :List of 2
..$ : num [1:1000] 1.25 1.5 1.58 1.54 1.6 ...
..$ : num [1:1000] 0.679 1.341 0.148 0.867 0.724 ...
$ :List of 2
..$ : num [1:1000] 0.526 1.545 1.848 0.711 0.697 ...
..$ : num [1:1000] 1.673 1.423 0.228 1.758 1.65 ...
有人可以告诉我最好的方法吗?
我的数据非常庞大,因此循环运行需要2个小时。 数据由3000个点(3000-1 neigh。)组成,每个点有+20.000个观测值。
答案 0 :(得分:1)
您是否尝试过以下命令?
lapply(list_a, function(x) list_obs[x])
答案 1 :(得分:1)
lapply(list_a,FUN=function(x)lapply(x,FUN=function(y)list_obs[y]))
但是,根据评论,可能有更好的方法来做到这一点
抱歉看到刚刚回答!
lapply(list_a,FUN=function(x)list_obs[x])
更好!