[英]efficient way to create a list of lists
我正在使用lapply和mapply創建一堆操作。 我的數據由列表組成。
對於其中一種計算,我在如何使用任何apply函數方面遇到一些問題。 這是我想做的一個例子:
#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
的點
list_obs中的位置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相鄰)組成,每個點具有+20.000個觀測值。
您是否嘗試過以下命令?
lapply(list_a, function(x) list_obs[x])
lapply(list_a,FUN=function(x)lapply(x,FUN=function(y)list_obs[y]))
但是,根據評論,可能會有更好的方法
抱歉看到剛才回答了!
lapply(list_a,FUN=function(x)list_obs[x])
更好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.