[英]dplyr::mutate: temporary expensive variable as input to several other operations, rowwise
[英]Set operations with dplyr::rowwise
我正在尝试对包含字符向量作为列表元素的列表变量执行set操作(intersect,union,setdiff,setequal)。 例如,
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)
就像是
df %>% rowwise() %>% mutate(z = setdiff(x, y))
似乎工作。 但不是
df %>% rowwise() %>% mutate(z = intersect(x, y))
使用intersect()给出错误消息:
错误:不兼容的大小(2),期望1(组大小)或1
intersect()
为第二行返回多个元素,您需要将其包装为列表以适合单元格:
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]>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.