How to use Indexing Operators in List in R

R has main 3 indexing operators. They are as follows :
  1. [ ] = always returns a list with a single element.
  2. [[ ]] = returns a object of the class of item contained in the list.
  3. $ = 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 )
a = dat["str"]
Result : "list".  a is a list.
b = dat[["str"]]
Result : "character". b is a character vector.
c = dat$str
Result : "character". c is a character vector.
R Indexing Operators
Important Note
Both $ and [[ ]] works same. But it is advisable to use [[ ]] in functions and loops. 
How to extract a list of list

R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
0 Response to "How to use Indexing Operators in List in R"

Post a Comment

Next → ← Prev