我有以下几点:
temp <- structure(list(x = list(1:10, 1:10), y = list(c(3L, 9L, 10L,
8L, 1L), c(1L, 3L, 5L, 2L, 4L))), .Names = c("x", "y"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -2L))
> temp
# A tibble: 2 x 2
x y
<list> <list>
1 <int [10]> <int [5]>
2 <int [10]> <int [5]>
我想创建一个新列z
,它是列x
和y
中列表元素的setdiff,这样temp$z
应输出为:
> temp$z
[[1]]
[1] 2 4 5 6 7
[[2]]
[1] 6 7 8 9 10
和temp将更新为:
> temp
# A tibble: 2 x 3
x y z
<list> <list> <list>
1 <int [10]> <int [5]> <int [5]>
2 <int [10]> <int [5]> <int [5]>
PS:一个dplyr解决方案会很棒! : - )
答案 0 :(得分:2)
您可以在Map
中使用mutate
:
temp %>% mutate(z=Map(setdiff, x, y))
# # A tibble: 2 x 3
# x y z
# <list> <list> <list>
# 1 <int [10]> <int [5]> <int [5]>
# 2 <int [10]> <int [5]> <int [5]>
temp %>% mutate(z=Map(setdiff, x, y)) %>% pull(z)
# [[1]]
# [1] 2 4 5 6 7
#
# [[2]]
# [1] 6 7 8 9 10
答案 1 :(得分:1)
您可以在mutate中使用purrr::map2
。
library(dplyr)
library(purrr)
temp %>% mutate(z = map2(x, y, setdiff))
#> # A tibble: 2 x 3
#> x y z
#> <list> <list> <list>
#> 1 <int [10]> <int [5]> <int [5]>
#> 2 <int [10]> <int [5]> <int [5]>
答案 2 :(得分:1)
或者只是我们在它的基地:)
within(temp,z<-Map(setdiff, x, y))