如何在R中合并两个列表?

时间:2021-07-06 20:24:22

I have two lists:

我有两个列表:

l1 = list(2, 3)
l2 = list(4)

I want a third list:

我想要第三个清单:

list(2, 3, 4).

How can I do it in simple way. Although I can do it in for loop, but I am expecting a one liner answer, or maybe an in-built method.

我怎么能用简单的方法做这件事呢?虽然我可以在for循环中这样做,但是我希望得到一个线性的答案,或者一个内建的方法。

Actually, I have a list:
list(list(2, 3), list(2, 4), list(3, 5), list(3, 7), list(5, 6), list(5, 7), list(6, 7)).
After computing on list(2, 3) and list(2, 4), I want list(2, 3, 4).

实际上,我有一个列表:list(list(2,3)、list(2,4)、list(3,5)、list(3,7)、list(list(5,6)、list(5,7)、list(list(6,7))。在计算了列表(2,3)和列表(2,4)之后,我想要列表(2,3,4)。

2 个解决方案

#1


29  

c can be used on lists (and not only on vectors):

c可以在列表上使用(不仅仅是在向量上):

# you have
l1 = list(2, 3)
l2 = list(4)

# you want
list(2, 3, 4)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

# you can do
c(l1, l2)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

If you have a list of lists, you can do it (perhaps) more comfortably with do.call, eg:

如果你有一个列表,你可以用do(也许)做得更舒服。调用,例如:

  do.call(c, list(l1, l2))`

#2


12  

We can use append

我们可以使用附加

append(l1, l2)

It also has arguments to insert element at a particular location.

它还具有在特定位置插入元素的参数。

#1


29  

c can be used on lists (and not only on vectors):

c可以在列表上使用(不仅仅是在向量上):

# you have
l1 = list(2, 3)
l2 = list(4)

# you want
list(2, 3, 4)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

# you can do
c(l1, l2)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

If you have a list of lists, you can do it (perhaps) more comfortably with do.call, eg:

如果你有一个列表,你可以用do(也许)做得更舒服。调用,例如:

  do.call(c, list(l1, l2))`

#2


12  

We can use append

我们可以使用附加

append(l1, l2)

It also has arguments to insert element at a particular location.

它还具有在特定位置插入元素的参数。