R Data Science: R Programming A-Z: R For Data Science With Real Exercises!
R has main 3 indexing operators. They are as follows :
- [ ] = always returns a list with a single element.
- [[ ]] = returns a object of the class of item contained in the list.
- $ = returns elements from list that have names associated with it, not necessarily same class
dat <- list( str='R', vec=c(1,2,3), bool=TRUE )Result : "list". a is a list.
a = dat["str"]
b = dat[["str"]]Result : "character". b is a character vector.
c = dat$strResult : "character". c is a character vector.
|R Indexing Operators|
Both $ and [[ ]] works same. But it is advisable to use [[ ]] in functions and loops.How to extract a list of list