列表索引中[[]]和$之间有什么区别?

时间:2022-06-20 13:25:48

Can anyone explain why list1 and list2 below are not identical?

任何人都可以解释为什么下面的list1和list2不相同?

list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1
list1
$level1
level2 
     1 

list2 <- list()
list2$level1$level2 <- 1
list2
$level1
$level1$level2
[1] 1

1 个解决方案

#1


17  

This is occurring because you are assigning a length 1 vector to a NULL atomic vector.

发生这种情况是因为您要将长度为1的向量分配给NULL原子向量。

From help(Extract) -

来自帮助(提取) -

When $<- is applied to a NULL x, it first coerces x to list(). This is what also happens with [[<- if the replacement value value is of length greater than one: if value has length 1 or 0, x is first coerced to a zero-length vector of the type of value.

当$ < - 应用于NULL x时,它首先将x强制转换为list()。这也是[[< - 如果替换值的长度大于1的情况:如果值的长度为1或0,则x首先被强制为值类型的零长度向量。

Change the assignment to ... <- 1:2 (or something other than a length 0 or 1 vector) and you will get the same result in both code blocks.

将赋值更改为... < - 1:2(或长度为0或1的向量之外的其他值),您将在两个代码块中得到相同的结果。

list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1:2
list1
# $level1
# $level1$level2
# [1] 1 2

list2 <- list()
list2$level1$level2 <- 1:2
list2
# $level1
# $level1$level2
# [1] 1 2

A simpler example of this, as mentioned by @alexis_laz in the comments, is just to begin with a NULL atomic vector and look at what happens.

正如@alexis_laz在评论中所提到的,一个更简单的例子就是从一个NULL原子向量开始,看看会发生什么。

x <- NULL
## assign a length 1 vector --> atomic result
x[["lev1"]] <- 1
x
# lev1 
#    1 

y <- NULL
## assign a length > 1 vector --> list result
y[["lev1"]] <- 1:2
y
# $lev1
# [1] 1 2

The result from $<- is always a list so I have omitted it here.

$ < - 的结果总是一个列表,所以我在这里省略了它。

#1


17  

This is occurring because you are assigning a length 1 vector to a NULL atomic vector.

发生这种情况是因为您要将长度为1的向量分配给NULL原子向量。

From help(Extract) -

来自帮助(提取) -

When $<- is applied to a NULL x, it first coerces x to list(). This is what also happens with [[<- if the replacement value value is of length greater than one: if value has length 1 or 0, x is first coerced to a zero-length vector of the type of value.

当$ < - 应用于NULL x时,它首先将x强制转换为list()。这也是[[< - 如果替换值的长度大于1的情况:如果值的长度为1或0,则x首先被强制为值类型的零长度向量。

Change the assignment to ... <- 1:2 (or something other than a length 0 or 1 vector) and you will get the same result in both code blocks.

将赋值更改为... < - 1:2(或长度为0或1的向量之外的其他值),您将在两个代码块中得到相同的结果。

list1 <- list()
lev1 <- "level1"
lev2 <- "level2"
list1[[lev1]][[lev2]] <- 1:2
list1
# $level1
# $level1$level2
# [1] 1 2

list2 <- list()
list2$level1$level2 <- 1:2
list2
# $level1
# $level1$level2
# [1] 1 2

A simpler example of this, as mentioned by @alexis_laz in the comments, is just to begin with a NULL atomic vector and look at what happens.

正如@alexis_laz在评论中所提到的,一个更简单的例子就是从一个NULL原子向量开始,看看会发生什么。

x <- NULL
## assign a length 1 vector --> atomic result
x[["lev1"]] <- 1
x
# lev1 
#    1 

y <- NULL
## assign a length > 1 vector --> list result
y[["lev1"]] <- 1:2
y
# $lev1
# [1] 1 2

The result from $<- is always a list so I have omitted it here.

$ < - 的结果总是一个列表,所以我在这里省略了它。