Select first element of nested list

后端 未结 5 1263
心在旅途
心在旅途 2020-12-02 06:21

Let\'s say I have a list like this:

x = list(list(1,2), list(3,4), list(5,6))

I would like a list that contains only the first elements of

5条回答
  •  失恋的感觉
    2020-12-02 07:06

    Not exactly a short notation, but this can also be done with a fold:

    Reduce(function(a, b) c(a, b[1]), x, init = c()) 
    
    # [[1]]
    # [1] 1
    # 
    # [[2]]
    # [1] 3
    # 
    # [[3]]
    # [1] 5
    

提交回复
热议问题