phdj
phdj

Reputation: 199

Subtracting list inside data.frame with list from another data.frame

I have two data frames that display a month and a list of ids in each row. They look like this:

dataframe A:

Month   ID
2016-03 1,2,3
2016-04 4,5,6
2016-05 7,8,9

dataframe B:

Month   ID
2016-03 2,3,4 
2016-04 5,6,7
2016-05 8,9,10

Seems simple, and perhaps I'm overthinking it, but I'm having trouble subtracting the corresponding rows from dataframe B from dataframe A.

Ultimate goal is to get the count of ids per row from dataframe A after dataframe B is removed.

So the resulting dataframe would look like:

Month   ID
2016-03 1 
2016-04 4
2016-05 7

and my count would be 1, 1, 1.

Thanks in advance for the help!

Update:

The values in the "ID" column are list objects like:

c("1", "2", "3")

Upvotes: 0

Views: 308

Answers (1)

thelatemail
thelatemail

Reputation: 93813

Use setdiff once you have appropriate vectors for each Month:

result <- Map(setdiff, A$ID, B$ID[match(A$Month,B$Month)] ))
#[[1]]
#[1] 1
#
#[[2]]
#[1] 4
#
#[[3]]
#[1] 7

If you need the lengths you can easily do:

lengths(result)
#[1] 1 1 1

Where, the data used was:

A <- structure(list(Month = c("2016-03", "2016-04", "2016-05"), ID = list(
    c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))), .Names = c("Month", 
"ID"), row.names = c(NA, -3L), class = "data.frame")
B <- structure(list(Month = c("2016-03", "2016-04", "2016-05"), ID = list(
    c(2, 3, 4), c(5, 6, 7), c(8, 9, 10))), .Names = c("Month", 
"ID"), row.names = c(NA, -3L), class = "data.frame")

Upvotes: 2

Related Questions