R has main 3 indexing operators. They are as follows :
- [ ] = always returns a list with a single element.
- [[ ]] = returns a list element
- $ = returns elements from list that have names associated with it, not necessarily same class
Examples
dat <- list( str='R', vec=c(1,2,3), bool=TRUE )
a = dat["str"]
a
class(a)
b = dat[["str"]]
b
class(b)
c = dat$str
c
class(b)
![]() |
R Indexing Operators |
Both $ and [[ ]] works same. But it is advisable to use [[ ]] in functions and loops.How to extract a list of list
dat1[[c("Bal02","ivtable")]]
dat1$Bal02$ivtable