我在R中构建一个列表列表,我使用它的唯一方法是使用嵌套的fors。问题是它们需要4秒钟才能运行,因此速度极慢。这就是我构建它们的方式:
tt = vector("list", length(dp$id))
for (idx in seq_along(dp$id)) {
pos = dp$id[idx]
position = dp[id == pos]$pdim
tt[[idx]] = list(
a = unbox(pos),
b = list()
)
tmp = positionData[positionId == position]
tls = vector("list", nrow(tmp))
if (nrow(tmp)) {
for (row in 1:nrow(tmp)) {
tls[[row]] = list(
c = unbox(tmp[row]$d),
d = unbox(tmp[row]$c)
)
}
tt[[idx]]$b = tls
}
}
有没有办法替换两者以更快地构建列表?
编辑:样本数据
dp = data.table(id =c(5632,5633, 5634, 5635, 5636), pdim = c(2103, 2048, 2093, 2069, 2086))
positionData = data.table(
positionId = c(2048, 2069, 2086, 2093, 2103, 2048, 2069, 2086, 2093, 2103, 2048, 2069, 2086, 2093, 2103, 2048, 2069, 2086, 2093, 2103, 2048, 2069, 2086, 2093, 2103, 2048),
d = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0),
c = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
答案 0 :(得分:1)
每个tt子列表的b
(总共5个)是data.frame
而不是列表?如果你能忍受这个,你肯定可以避免第二次循环:
library(dplyr)
tt = list()
for (idx in seq_along(dp$id)) {
pos = dp$id[idx]
position= dp[id == pos]$pdim
tt[[idx]] = list(a = pos,b = list())
tmp = positionData[positionId == position]
tls<-transmute(tmp,c=d,d=c)
tt[[idx]]$b = tls
}