简体   繁体   中英

Set operations with dplyr::rowwise

I'm trying to perform set operations (intersect, union, setdiff, setequal) on list-variables that have character vectors as the list elements. For example,

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)

Something like

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

seems to work. But not

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

using intersect() gives the error message:

Error: incompatible size (2), expecting 1 (the group size) or 1

intersect() returns more than one element for the second row, you need to wrap it as a list to fit a 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]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM