R Tibble:对列表列的操作
我有以下几点:R Tibble:对列表列的操作,r,tibble,R,Tibble,我有以下几点: 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
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]>
而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]>
>温度
#一个tibble:2x3
x y z
1.
2.
PS:dplyr解决方案将非常好!:-) 您可以在
mutate
中使用Map
:
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
temp%>%变异(z=Map(setdiff,x,y))
##tibble:2 x 3
#x y z
#
# 1
# 2
温度%>%突变(z=Map(setdiff,x,y))%>%pull(z)
# [[1]]
# [1] 2 4 5 6 7
#
# [[2]]
# [1] 6 7 8 9 10
您可以在mutate中使用
库(dplyr)
图书馆(purrr)
温度%>%突变(z=map2(x,y,setdiff))
#>#tibble:2 x 3
#>x y z
#>
#> 1
#> 2
或者我们在这里的时候,就在这里:)
在(temp,我不确定dplyr,但使用purr,您可以执行类似于temp$z=pmap(temp,~setdiff(.x,.y))
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
within(temp,z<-Map(setdiff, x, y))