Reputation: 101
I have a list that contains multiple lists of three vectors
That prints:
[[1]]
[[1]][[1]]
[1] 1 2
[[1]][[2]]
[1] 1 3
[[1]][[3]]
[1] 2 3
[[2]]
[[2]][[1]]
[1] 4 5
[[2]][[2]]
[1] 5 6
[[2]][[3]]
[1] 4 6
I want to create two vectors:
c(1, 2, 3), c(4, 5, 6)
I have also tried to unique(unlist(x))
which works but loses the references to the positions within the list. I have multiple lists within my lists, so when I apply unique(unlist(list(x)))
it just creates one string. I want to keep my vector c(1, 2, 3)
separate from the next list of c(4, 5, 6)
Upvotes: 1
Views: 77
Reputation: 388817
You are almost there. You just need to apply your solution to all the lists separately.
lapply(lst, function(x) unique(unlist(x)))
#[[1]]
#[1] 1 2 3
#[[2]]
#[1] 4 5 6
data
lst <- list(list(c(1, 2), c(1, 3), c(2, 3)), list(c(4,5), c(5, 6), c(4, 6)))
Upvotes: 1