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
library(dplyr)
library(purrr)
z %>%
mutate(x = map(x, unique))
In base R
, it would be
z$x <- lapply(z$x, unique)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.