当前位置: 动力学知识库 > 问答 > 编程问答 >

Proper way to access list elements in R

问题描述:

This question already has an answer here:

  • The difference between [] and [[]] notations for accessing the elements of a list

    8 answers

网友答案:

All these methods give different outputs

[ ] returns a list

[[ ]] returns the object which is stored in list

If it is a named list, then

List$name or List[["name"]] will return same as List[[ ]]

While List["name"] returns a list, Consider the following example

> List <- list(A = 1,B = 2,C = 3,D = 4)
> List[1]
$A
[1] 1

> class(List[1])
[1] "list"
> List[[1]]
[1] 1
> class(List[[1]])
[1] "numeric"
> List$A
[1] 1
> class(List$A)
[1] "numeric"
> List["A"]
$A
[1] 1

> class(List["A"])
[1] "list"
> List[["A"]]
[1] 1
> class(List[["A"]])
[1] "numeric"
分享给朋友:
您可能感兴趣的文章:
随机阅读: