3

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 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.