R +将向量列表合并为一个向量 [英] R + combine a list of vectors into a single vector
本文介绍了R +将向量列表合并为一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只有一个数字矢量列表,我想将它们组合成一个矢量.但是我做不到.此列表可以具有一个在整个list元素中相同的元素.最终向量不应将它们相加两次.这是一个示例:
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
我想要这样的最终结果
>result
[1] 1 2 4 5 9 1
我尝试做以下事情,而不用担心它的出现:
I tried doing following things, without worrying about the repition:
>vec<-vector()
>sapply(lst, append,vec)
和
>vec<-vector()
>sapply(lst, c, vec)
他们都没有工作.有人可以帮我吗?
None of them worked. Can someone help me on this?
谢谢.
推荐答案
一种比上面提出的解决方案快的解决方案:
A solution that is faster than the one proposed above:
vec<-unlist(lst)
vec[which(c(1,diff(vec)) != 0)]
这篇关于R +将向量列表合并为一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文