Proper way to access list elements in R [duplicate] Proper way to access list elements in R [duplicate] r r

Proper way to access list elements in R [duplicate]


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"