我有一个这样的示例数据框
我正在尝试使用以下代码找到2列coauthors
和nacoauthors
之间的交集
interscout =
sample_test %>%
mutate( commonauth = intersect( coauthors, nacoauthors) )
我得到了这个输出
我不确定为什么我无法使用intersect
中的mutate
获取公共交集。
理想情况下,最后一行应该为空,第二行应该只有JAMES M ANDERSON
交叉。
以下是结构的代码。
> dput(sample_test)
structure(list(fname = c("JACK", "JACK", "JACK"), lname = c("SMITH",
"SMITH", "SMITH"), cname = c("JACK SMITH", "JACK A SMITH", "JACK B SMITH"
), coauthors = list(c("AMEY S BAILEY", "JAMES M ANDERSON"), "JAMES M ANDERSON",
"JOHN MURRAY"), nacoauthors = list(c("AMEY S BAILEY", "JAMES M ANDERSON"
), c("AMEY S BAILEY", "JAMES M ANDERSON"), c("AMEY S BAILEY",
"JAMES M ANDERSON"))), row.names = c(NA, -3L), vars = list(fname,
lname), drop = TRUE, indices = list(0:2), group_sizes = 3L, biggest_group_size = 3L, labels = structure(list(
fname = "JACK", lname = "SMITH"), class = "data.frame", row.names = c(NA,
-1L), vars = list(fname, lname), drop = TRUE, .Names = c("fname",
"lname")), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), .Names = c("fname", "lname", "cname", "coauthors", "nacoauthors"
))
答案 0 :(得分:2)
如果您添加rowwise()
并将变异列设为list
,那么它将起作用:
interscout <- sample_test %>%
ungroup() %>%
rowwise() %>%
mutate( commonauth = list( intersect(coauthors, nacoauthors) ) )
FWIW如果我不包括rowwise()
,我会Error: not compatible with STRSXP