我想计算var1 [i]和union(var2 [1],...,var2 [i])的交集。
使用此数据
var1 <- list('2003' = 1:3, '2004' = c(4:3), '2005' = c(6,4,1), '2006' = 1:4 )
var2 <- list('2003' = 1:3, '2004' = c(4:5), '2005' = c(2,3,6), '2006' = 2:3 )
我想用以下内容填充结果列表:
1. intersect(var1$2003,var2$2003) 2. intersect(var1$2004,union(var2$2003,var2$2004)) 3. intersect(var1$2005,union(var2$2005(union(var2$2003,var2$2004))))
等等,直到2012年(未在示例中显示)
答案 0 :(得分:3)
这是你想要的吗?
# create the data
var1 <- list('2003' = 1:3, '2004' = c(4:3), '2005' = c(6,4,1), '2006' = 1:4 )
var2 <- list('2003' = 1:3, '2004' = c(4:5), '2005' = c(2,3,6), '2006' = 2:3 )
# A couple of nested lapply statements
lapply(setNames(seq_along(var1), names(var1)),
function(i,l1,l2) length(intersect(l1[[i]], Reduce(union,l2[1:i]))),
l1 = var1,l2=var2)
$`2003`
[1] 3
$`2004`
[1] 2
$`2005`
[1] 3
$`2006`
[1] 4
请注意,Reduce(union,var2)
通过使用var2
连续组合元素来缩小列表union
(请参阅?Reduce
)
Reduce(union,var2)
[1] 1 2 3 4 5 6
使用accumulate = T
Reduce
参数
lapply(mapply(intersect,var1, Reduce(union, var2, accumulate=T)),length)
因为 -
Reduce(union, var2, accumulate = T)
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] 1 2 3 4 5
##
## [[3]]
## [1] 1 2 3 4 5 6
##
## [[4]]
## [1] 1 2 3 4 5 6