【发布时间】:2020-05-05 20:35:34
【问题描述】:
我有一个在列表列中包含重复值的数据框,我只想保留每个唯一值的第一次出现。
假设我有以下小标题:
df <- tribble(
~x, ~y,
1, tibble(a = 1:2, b = 2:3),
2, tibble(a = 1:2, b = 2:3),
3, tibble(a = 0:1, b = 0:1)
)
df
#> # A tibble: 3 x 2
#> x y
#> <dbl> <list>
#> 1 1 <tibble [2 x 2]>
#> 2 2 <tibble [2 x 2]>
#> 3 3 <tibble [2 x 2]>
期望的结果是:
desired_df
#> # A tibble: 2 x 2
#> x y
#> <dbl> <list>
#> 1 1 <tibble [2 x 2]>
#> 2 3 <tibble [2 x 2]>
y 不是列表列,我可以使用distinct(df, y, .keep_all = TRUE),但该功能不正确支持列表列,如图所示:
distinct(df, y, .keep_all = TRUE)
#> Warning: distinct() does not fully support columns of type `list`.
#> List elements are compared by reference, see ?distinct for details.
#> This affects the following columns:
#> - `y`
#> # A tibble: 3 x 2
#> x y
#> <dbl> <list>
#> 1 1 <tibble [2 x 2]>
#> 2 2 <tibble [2 x 2]>
#> 3 3 <tibble [2 x 2]>
有什么“干净”的方式来实现我想要的吗?
【问题讨论】:
-
我希望我的解决方案是一个干净的选择
标签: r dataframe dplyr distinct tidyverse