1

I have a list contains number of vectors with different length as below:

lst <- list(c(1,2), c(1,2), c(4,5,10,11,12,13), c(7,8,9))
lst

[[1]]
[1] 1 2

[[2]]
[1] 1 2

[[3]]
[1]  4  5 10 11 12 13

[[4]]
[1] 7 8 9

[[5]]
[1] 7 8 9

how can I combine and remove repeated vectors to be similar to the list below:

[[1]]
[1] 1 2

[[2]]
[1]  4  5 10 11 12 13

[[3]]
[1] 7 8 9

for repeated vectors I can use unique function.

JanLauGe
  • 2,297
  • 2
  • 16
  • 40
Noor
  • 365
  • 2
  • 13
  • The question is not clear. Do you want to remove all duplicated numbers no matter where they occur or does any vector need to be completely contained inside another vector in order to be removed? one of the current answers handles the first case. – talat Jul 04 '17 at 10:48
  • So, you want the sublists `[1] 10 11 12 13` also removed because these lists are contained in `[1] 4 5 10 11 12 13`? – Karsten W. Jul 04 '17 at 11:16
  • Take a look at this answer, it might solve your problem: https://stackoverflow.com/a/27521122/3521006 – talat Jul 04 '17 at 11:46

2 Answers2

1

Here is one option. We unlist the list, get a logical vector with duplicated, relist it to a list having the same skeleton as the original list, subset the 'l1' based on the logical list with Map and Filter out the list elements having 0 elements.

Filter(length, Map(`[`, l1, relist(!duplicated(unlist(l1)), skeleton = l1)))
#[[1]]
#[1] 1 2

#[[2]]
#[1]  4  5 10 11 12 13

#[[3]]
#[1] 7 8 9

#[[4]]
#[1] 14 15

#[[5]]
#[1] 19 20

data

l1 <- list(1:2, 1:2, c(4, 5, 10, 11, 12, 13), 7:9, 7:9, 10:13, 10:13, 
  c(4, 10, 11, 12, 13), 14:15, 19:20)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

I have edited your answer to give a reproducible example. Please let me know if this does not correctly illustrate your problem anymore. If it does, this is trivial:

unique(lst)
JanLauGe
  • 2,297
  • 2
  • 16
  • 40