I am trying to perform set operations (intersect, union, setdiff, setequal) in list variables that have character vectors as list items. For instance,
library(dplyr)
list1 = list(c('a', 'b'), c('x', 'y', 'z'))
list2 = list(c('b'), c('x', 'z'))
df = data_frame(x = list1, y = list2)
Sort of
df %>% rowwise() %>% mutate(z = setdiff(x, y))
seems to work. But not
df %>% rowwise() %>% mutate(z = intersect(x, y))
using intersect (), it gives an error message:
Error: incompatible size (2) expecting 1 (group size) or 1
source
share