简体   繁体   English

从 R 中向量内的列表中删除重复项

[英]Remove duplicates from lists within a vector in R

I have a vector of lists like the following sample:我有一个列表向量,如下例所示:

library(tidyverse)

z <- tribble(
  ~x,
  c(10, 10, 64),
  c(22, 22),
  c(5, 9, 9),
  c(55, 55),
  c(76, 65)
)

I'm trying to reduce each list to include only cases with unique values.我正在尝试减少每个列表以仅包含具有唯一值的案例。 Here's the output I'm looking for:这是我正在寻找的输出:

y <- tribble(
  ~x,
  c(10, 64),
  c(22),
  c(5, 9),
  c(55),
  c(76, 65)
)

Of course I can't post the actual output and have to write it out as a new data set for this example because it looks like this otherwise:当然,我无法发布实际输出,并且必须将其作为此示例的新数据集写出来,否则它看起来像这样:

# A tibble: 5 x 1
  x        
  <list>   
1 <dbl [3]>
2 <dbl [2]>
3 <dbl [3]>
4 <dbl [2]>
5 <dbl [2]>

We can loop over the list with map and apply unique我们可以使用map遍历list并应用unique

library(dplyr)
library(purrr)
z %>% 
   mutate(x = map(x, unique))

In base R , it would bebase R ,它将是

z$x <- lapply(z$x, unique)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM