I have a single list of numeric vector and I want to combine them into one vector. But I am unable to do that. This list can have one element common across the list element. Final vector should not add them twice. Here is an example:
>lst
`1`
[1] 1 2
`2`
[2] 2 4 5
`3`
[3] 5 9 1
I want final result as this
>result
[1] 1 2 4 5 9 1
I tried doing following things, without worrying about the repition:
>vec<-vector()
>sapply(lst, append,vec)
and
>vec<-vector()
>sapply(lst, c, vec)
None of them worked. Can someone help me on this?
Thanks.
unique
will be fine-grained enough;unique
could quite easily remove more than the 1 common element between adjacent list components. Noteunique(unlist(lst))
wouldn't give what the OP wants.unique()
would strip one of the1
s which the OP claims should be in the output.unique(do.call(c, lst))
. According to the gospel of @MatthewLundberg,rle(do.call(c, lst))$values
. Based on my benchmark, Matthew's solution is faster.