我的列表有问题。我有两个清单:
List1:
[[1]]
[1] 176 177 178 179 180
[[2]]
[1] 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
[21] 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
[[3]]
[1] 260 261 262 263 264 265
[[4]]
[1] 293 294 295 296 297 298 299 300 301 302 303 304
[[5]]
[1] 393 394 395 396 397 398
List 2:
[[1]]
[1] 99 100 101 102 103
[[2]]
[1] 260 261 262 263 264 265
[[3]]
[1] 293 294 295 296 297 298 299 300
[[4]]
[1] 390 391 392 393 394 395
现在我想使用combined overlapping lists of list 1 and list 2
创建一个新列表。它应该是这样的:
List 3:
[[1]]
[1] 260 261 262 263 264 265
[[2]]
[1] 293 294 295 296 297 298 299 300 301 302 303 304
[[3]]
[1] 390 391 392 393 394 395 396 397 398
我很乐意接受想法!
答案 0 :(得分:0)
以下是我要求的结果:
list1<-list(176:180, 222:256, 260:265, 293:304, 393:398)
list2<-list(99:103, 260:265, 293:300, 390:395)
result <- list()
for (x1 in list1) {
for (x2 in list2) {
if (any(x1 %in% x2)) {
result[[length(result)+1]] <- union(x1, x2)
}
}
}
答案 1 :(得分:0)
听起来是您的列表,范围列表,想法是使用IRanges
包。
您可以这样做:
首先我创建我的范围:
query <- IRanges(c(176,222,260,293,393),c(180,256,265,304,398))
subject <- IRanges(c(99,260,293,390),c(103,265,300,395))
query
IRanges of length 5
start end width
[1] 176 180 5
[2] 222 256 35
[3] 260 265 6
[4] 293 304 12
[5] 393 398 6
subject
IRanges of length 4
start end width
[1] 99 103 5
[2] 260 265 6
[3] 293 300 8
[4] 390 395 6
比我找到Overlaps
tree <- IntervalTree(subject)
res <- findOverlaps(query, tree)
res
Hits of length 3
queryLength: 5
subjectLength: 4
queryHits subjectHits
<integer> <integer>
1 3 2 ## third element in 2 second element
2 4 3
3 5 4