我有一组数字坐标,它们代表矢量的开始和结束位置,将根据连续区域的表进行检查,以查看它们存在于哪个区域中。对于每对开始和结束位置,都有不同区域表进行检查。当前,数字坐标存储在一对均等索引的向量中(这样,起始[1]对以end [1]对),将对照table [[1]]进行检查。我已经有了for循环,但是想知道是否有一种更快的方法(可能/可能使用apply系列的某个成员)考虑到向量和列表元素的索引始终相同。
使用示例可能更容易理解:
starts <- c(7, 163); ends <- c(15, 165)
df1 <- data.frame(seq(from = 1, to = 91, by = 10),
seq(from = 10, to = 100, by = 10), paste0('Region', 1:10))
df2 <- data.frame(seq(from = 101, to = 191, by = 10),
seq(from = 110, to = 200, by = 10), paste0('Region', 1:10))
mylist <- list(df1, df2)
# With a for loop, I could iterate through as follows:
for (n in 1:length(starts)) {
regions <- which(starts[n] >= mylist[[n]][, 1] & starts[n] <= mylist[[n]][, 2]):which(ends[n] >= mylist[[n]][, 1] & ends[n] <= mylist[[n]][, 2])
print(mylist[[n]][regions, 3])
}
尽管上述方法可以正常工作,但我可能会运行成千上万次,因此,如果可能,请避免使用for循环。
我可能应该注意,lapply
对此不起作用,因为它会根据当前列表检查每个向量索引,反之亦然,并用“ apply”进行检查。
我认为我可以保存一个新列表,其中两个开始索引和结束索引以及数据框为子列表(即mylist <- list(list(starts[1], ends[1], df1), list(starts[2])...
),但是我想知道是否还有更清洁的方法?
答案 0 :(得分:0)
弄清楚了-万一将来有人碰到这个问题,请将X
中的lapply
设置为1:length(list/vector)
即可。
使用上面的示例,而不是for循环,请尝试:
lapply(X = 1:length(mylist),
FUN = function(n) { mylist[[n]]
[which(starts[n] >= mylist[[n]][, 1] &
starts[n] <= mylist[[n]][,2]):
which(ends[n] >= mylist[[n]][, 1] &
ends[n] <= mylist[[n]][, 2]), 3] } )