Define operations using dplyr :: rollise

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

+4
source share
1 answer

intersect() returns more than one element for the second row, you need to wrap it as a list suitable for the cell:

df %>% rowwise() %>% mutate(z = list(intersect(x, y)))

# Source: local data frame [2 x 3]
# Groups: <by row>

#           x         y         z
#      <list>    <list>    <list>
# 1 <chr [2]> <chr [1]> <chr [1]>
# 2 <chr [3]> <chr [2]> <chr [2]>
+4
source

Source: https://habr.com/ru/post/1651088/


All Articles