Remove duplicates in a nested list

Viewed 190

I have a large list of lists where I want to remove duplicated elements in each list. Example:

x <- list(c("A", "A", "B", "C"), c("O", "C", "A", "Z", "O"))

x 

[[1]]
[1] "A" "A" "B" "C"

[[2]]
[1] "O" "C" "A" "Z" "O"

I want the result to be a list that looks like this, where duplicates within a list are removed, but the structure of the list remains.

[[1]]
[1] "A" "B" "C"

[[2]]
[1] "O" "C" "A" "Z"

My main strategy has been to use rapply (also tried lapply) to identify duplicates and remove them. I tried:

x[rapply(x, duplicated) == T]

but received the following error:

"Error: (list) object cannot be coerced to type 'logical'"

Does anyone know a way to solve this issue?

Thanks!

1 Answers

We can use lapply with unique

lapply(x, unique)
#[[1]]
#[1] "A" "B" "C"

#[[2]]
#[1] "O" "C" "A" "Z"

The issue with rapply, is that it recursively applies the duplicated and then returns a single vector instead of a list of logical vectors

rapply(x, duplicated)
#[1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

Instead it can be

lapply(x, function(u) u[!duplicated(u)])
#[[1]]
#[1] "A" "B" "C"

#[[2]]
#[1] "O" "C" "A" "Z"
Related