如何在R中合并两个列表 [英] How to combine two lists in R

查看:230
本文介绍了如何在R中合并两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

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

我想要第三个列表:

list(2, 3, 4).

如何以简单的方式做到这一点. 尽管我可以在for循环中完成此操作,但是我期望一个衬套答案,或者一个内置方法.

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.

实际上,我有一个列表:
list(list(2, 3), list(2, 4), list(3, 5), list(3, 7), list(5, 6), list(5, 7), list(6, 7)).
list(2, 3)list(2, 4)上计算后,我想要list(2, 3, 4).

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).

推荐答案

c可以用于列表(不仅限于矢量):

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

# 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

如果您有一个列表列表,则可以使用do.call(也许)更轻松地完成它,例如:

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

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

这篇关于如何在R中合并两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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