0

我想计算 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 年(示例中未显示)

  • 免责声明:由于编辑,以下评论可能没有意义。
4

1 回答 1

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)减少列表var2union?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
于 2012-08-31T05:41:19.720 回答