比较R中两个列表的每个元素

时间:2014-04-16 16:36:55

标签: r list sapply

我在R中有一个数据集,结构如下:

data <- structure(list(Cust_ID = c("003", "023", "023", "023", "023", 
"041", "056", "056"), Record_Date = list(structure(16130, class = "Date"), 
    structure(c(16130, 16130, 16130, 16130), class = "Date"), 
    structure(c(16150, 16150, 16150, 16150), class = "Date"), 
    structure(c(16161, 16161, 16161, 16161), class = "Date"), 
    structure(c(16162, 16162, 16162, 16162), class = "Date"), 
    structure(16133, class = "Date"), structure(c(16088, 16088
    ), class = "Date"), structure(c(16095, 16095), class = "Date")), 
    Compare_Date = list(structure(16130, class = "Date"), structure(c(16130, 
    16150, 16161, 16162), class = "Date"), structure(c(16130, 
    16150, 16161, 16162), class = "Date"), structure(c(16130, 
    16150, 16161, 16162), class = "Date"), structure(c(16130, 
    16150, 16161, 16162), class = "Date"), structure(16133, class = "Date"), 
        structure(c(16088, 16095), class = "Date"), structure(c(16088, 
        16095), class = "Date"))), row.names = c(NA, -8L), class = "data.frame", .Names = c("Cust_ID", 
"Record_Date", "Compare_Date"))

  Cust_ID                Record_Date               Compare_Date
1     003                      16130                      16130
2     023 16130, 16130, 16130, 16130 16130, 16150, 16161, 16162
3     023 16150, 16150, 16150, 16150 16130, 16150, 16161, 16162
4     023 16161, 16161, 16161, 16161 16130, 16150, 16161, 16162
5     023 16162, 16162, 16162, 16162 16130, 16150, 16161, 16162
6     041                      16133                      16133
7     056               16088, 16088               16088, 16095
8     056               16095, 16095               16088, 16095

我想比较&#34; Record_Date&#34;的每个元素。和#34; Compare_Date的每个元素。&#34;我希望结果等于&#34; Compare_Date&#34;在&#34; Record_Date之后的14天内。&#34;我知道如何比较两个向量,但比较两个列表似乎给我带来了麻烦。我尝试过使用lapply或sapply,但这些只能一次遍历一个列表。

有没有人能轻松解决这个问题?我希望预期的输出看起来如下:

   Within14
1:        0
2:        2
3:        1
4:        0
5:        1
6:        0
7:        0
8:        0

1 个答案:

答案 0 :(得分:3)

也许,您正在寻找mapply。您可以编辑FUN mapply参数,以便在每个RDCD参数上执行您想要的操作。

mapply分别将data$Record_Datedata$Compare_Date中的每一个元素分别传递给RD CDFUN个参数。

mapply(FUN = function(RD, CD) {
    d <- as.numeric(CD - RD)
    sum(d > 0 & d < 15)
}, RD = data$Record_Date, CD = data$Compare_Date)
## [1] 0 0 2 1 0 0 1 0