Reputation: 4615
I have a dataset in R with the following structure:
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
I would like to compare each element of "Record_Date" and each element of "Compare_Date." I want the result to be equal to the amount of times the "Compare_Date" is within 14 days after the "Record_Date." I know how to compare two vectors, but comparing two lists seems to be giving me trouble. I have tried using lapply or sapply, but those can only loop through one list at a time.
Does anyone have an easy solution to this problem? I would expect the expected output to look like the following:
Within14
1: 0
2: 2
3: 1
4: 0
5: 1
6: 0
7: 0
8: 0
Upvotes: 1
Views: 2316
Reputation: 17189
Perhaps, you are looking for mapply
. You can edit the FUN
argument of mapply
to do what you want on each RD
and CD
argument.
mapply
passes one element each of data$Record_Date
and data$Compare_Date
into RD
and CD
arguments of FUN
respectively.
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
Upvotes: 3