![](/img/trans.png)
[英]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.