How to combine two lists in R

前端 未结 3 1601
长情又很酷
长情又很酷 2020-12-04 18:37

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 sim

3条回答
  •  没有蜡笔的小新
    2020-12-04 19:12

    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
    

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

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

提交回复
热议问题