选择嵌套列表的第一个元素 [英] Select first element of nested list

查看:70
本文介绍了选择嵌套列表的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的列表:

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 the nested list. I can do this by returning another list like so

x1 = lapply(x, function(l) l[[1]])

有快捷方式吗?

推荐答案

没有太多捷径,但是您可以这样做:

Not much of a shortcut, but you can do this:

lapply(x, `[[`, 1)
# [[1]]
# [1] 1
#
# [[2]]
# [1] 3
#
# [[3]]
# [1] 5

这篇关于选择嵌套列表的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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