简体   繁体   English

将向量列表附加到向量列表中

[英]append a list of vectors into a list of vectors

Let x be a list of vectors: x为向量列表:

 a <-list( c(1,2,3),   c(4,5,6), c(7,8) ,c(9) )
 b <-list(c(11,22,33),c(44,55,66), c(77,88) ,c(99) )
 x <- list(a=a,b=b)

My desired output is the following: 我想要的输出如下:

>foo(x)

 list(c(1,2,3,11,22,33),c(4,5,6,44,55,66),c(7,8,77,88),c(9,99)

Summary of Answers by the Akrun and gfgm Akrun和gfgm 的答案摘要

  1. Map(c, a, b)
  2. do.call(Map, c(f = "c", unname(x)))
  3. mapply(function(i, j){c(i,j)}, a, b)

This is a good use case for mapply() 这是mapply()好用例

a <-list( c(1,2,3),   c(4,5,6), c(7,8) ,c(9) )
b <-list(c(11,22,33),c(44,55,66), c(77,88) ,c(99) )

mapply(function(i, j){c(i,j)}, a, b)
#> [[1]]
#> [1]  1  2  3 11 22 33
#> 
#> [[2]]
#> [1]  4  5  6 44 55 66
#> 
#> [[3]]
#> [1]  7  8 77 88
#> 
#> [[4]]
#> [1]  9 99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM