dplyr 突变相交不起作用
dplyr mutate intersect not working
我有一个这样的示例数据框
我正在尝试使用以下代码
找到两列 coauthors
和 nacoauthors
之间的交集
interscout =
sample_test %>%
mutate( commonauth = intersect( coauthors, nacoauthors) )
我得到了这个输出
我不确定为什么我无法在 mutate
.
中使用 intersect
获取公共交集
理想情况下,最后一行应该是空的,第二行应该只有 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"
))
如果您添加 rowwise()
并将您的变异列设为 list
,它将起作用:
interscout <- sample_test %>%
ungroup() %>%
rowwise() %>%
mutate( commonauth = list( intersect(coauthors, nacoauthors) ) )
FWIW 如果我不包括 rowwise()
我得到 Error: not compatible with STRSXP
我有一个这样的示例数据框
coauthors
和 nacoauthors
之间的交集
interscout =
sample_test %>%
mutate( commonauth = intersect( coauthors, nacoauthors) )
我得到了这个输出mutate
.
intersect
获取公共交集
理想情况下,最后一行应该是空的,第二行应该只有 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"
))
如果您添加 rowwise()
并将您的变异列设为 list
,它将起作用:
interscout <- sample_test %>%
ungroup() %>%
rowwise() %>%
mutate( commonauth = list( intersect(coauthors, nacoauthors) ) )
FWIW 如果我不包括 rowwise()
我得到 Error: not compatible with STRSXP