R + combine a list of vectors into a single vector

后端 未结 6 780
死守一世寂寞
死守一世寂寞 2021-01-30 10:37

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. Fin

6条回答
  •  野的像风
    2021-01-30 10:44

    Another answer using Reduce().

    Create the list of vectors:

    lst <- list(c(1,2),c(2,4,5),c(5,9,1))
    

    Combine them into one vector

    vec <- Reduce(c,lst)
    vec
    # [1] 1 2 2 4 5 5 9 1
    

    Keep the repeated ones only once:

    unique(Reduce(c,lst))
    #[1] 1 2 4 5 9
    

    If you want to keep that repeated one at the end, You might want to use vec[which(c(1,diff(vec)) != 0)] as in @Rachid's answer

提交回复
热议问题