R + 将一个向量列表组合成一个向量 [英] R + combine a list of vectors into a single vector

查看:36
本文介绍了R + 将一个向量列表组合成一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字向量列表,我想将它们组合成一个向量.但我无法做到这一点.该列表可以有一个跨列表元素共有的元素.最终向量不应将它们添加两次.这是一个例子:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆