R中使用双方括号的子集列表 [英] Subsetting list in R using double square brackets
问题描述
在阅读了有关R中的子集列表的多个线程之后,我试图通过在Rstudio中四处闲逛来完全理解这一概念.我以为我理解了这个概念,直到遇到以下问题:
After reading several threads about subsetting lists in R, I tried to fully grasp this notion by fooling around in Rstudio. I thought I understood the concept until I came across the following:
x <- list(list(list(1), 2), list(list(list(3), 4), 5), 6)
为什么x[[1]]
返回一个包含两个元素的列表,而x[[1]][[1]]
也返回一个列表呢?
Why is it that x[[1]]
returns a list with two elements and x[[1]][[1]]
returns a list too?
推荐答案
使用两个额外的空格查看代码,以使结构更清晰:
Look at the code with two extra spaces to help make the structure more clear:
x <- list( list( list(1), 2), list(list(list(3), 4), 5), 6)
x的第一个元素是:list( list(1), 2)
#显然有两个元素的列表
The first element of x is: list( list(1), 2)
# clearly a list with two elements
该列表的第一个元素是:list(1)
#也是一个列表,但只有一个元素
And the first element of that list is: list(1)
# also a list but with one element
这篇关于R中使用双方括号的子集列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!