list R + 将向量列表合并为一个向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15515390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:58:48  来源:igfitidea点击:

R + combine a list of vectors into a single vector

rlistvectorappendsapply

提问by Rachit Agrawal

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:

我尝试做以下事情,而不用担心 reition:

>vec<-vector()
>sapply(lst, append,vec)

and

>vec<-vector()
>sapply(lst, c, vec)

None of them worked. Can someone help me on this?

他们都没有工作。有人可以帮我吗?

Thanks.

谢谢。

采纳答案by Rachit Agrawal

A solution that is faster than the one proposed above:

一种比上面提出的更快的解决方案:

vec<-unlist(lst)
vec[which(c(1,diff(vec)) != 0)]

回答by Paul Rougieux

Another answer using Reduce().

另一个答案使用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

如果你想在最后保留那个重复的,你可能想vec[which(c(1,diff(vec)) != 0)]在@Rachid 的回答中使用

回答by 0mn1

stack will do this nicely too, and looks more concise:

stack 也可以很好地做到这一点,并且看起来更简洁:

stack(lst)$values

回答by Matthew Lundberg

You want rle:

你想要rle:

rle(unlist(lst))$values

> lst <- list(`1`=1:2, `2`=c(2,4,5), `3`=c(5,9,1))
> rle(unlist(lst))$values
## 11 21 22 31 32 33 
##  1  2  4  5  9  1 

回答by MartijnVanAttekum

Doing it the tidyverse way:

以 tidyverse 的方式进行:

library(tidyverse)
lst %>% reduce(c) %>% unique

This uses the (uncapitalized) reduceversion from purrr in combination with pipes. Also note that if the list contains namedvectors, the final naming will be different depending on whether unlistor reducemethods are used.

这将reducepurrr的(未大写)版本与管道结合使用。另请注意,如果列表包含命名向量,则最终命名将根据是否使用unlistreduce方法而有所不同。

回答by Prradep

Benchmarking the two answers by Rachitand Martijn

RachitMartijn的两个答案进行基准测试

rbenchmark::benchmark(
  "unlist" = {
    vec<-unlist(a)
    vec[which(diff(vec) != 0)]
  },
  "reduce" = {
    a %>% reduce(c) %>% unique
  }
)

Output:

输出:

    test replications elapsed relative user.self sys.self user.child sys.child
2 reduce          100   0.036        3     0.036    0.000          0         0
1 unlist          100   0.012        1     0.000    0.004          0         0

Thisone clearly beat the other one.

一个明显击败了另一个。