0

I'm trying to take the row-wise set difference of two different list columns. So, line 1 column A would be a list of IDs for one year (m_lag), and line 2 column A would be a different list of IDs for the next year (m), and then finding the length of those set differences.

I've tried both dplyr techniques and using a for loop. Some example code below that I've tried so far:

for (i in 1:nrow(test)){
    m <- test[i,3][1]
    m_lag <- test[i,5][1]
    names(m_lag) <- "m"
  
    Diff1 <- setdiff(m_lag, m)
    Diff2 <- setdiff(m, m_lag)
  
    L1 <- length(Diff1)
    L2 <- length(Diff2)

}

test %>% mutate(Diff1 = setdiff(m_lag, m), Diff2 = setdiff(m, m_lag))

test %>% mutate(Diff1 = map2(m_lag, m, setdiff), Diff2 = map2(m, m_lag, setdiff))

I've also tried different variations of indexing, such as m[[1]] instead of m, and in the for loop using nrow() in place of length().

What I need at the end (eventually) is the length of each difference represented as an integer.

  • 2
    Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))` into a [code block]) and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jun 02 '23 at 21:34

0 Answers0