Chapter 7 List in R

R list is the object which contains element of different types such as strings,number etc. In R list can be created using list function.

Lets create a list:

Name<-c("Manish","Amit","Amul")
RollNum<-c(23,24,25)

sample_list<-list(Name,RollNum)

sample_list
## [[1]]
## [1] "Manish" "Amit"   "Amul"  
## 
## [[2]]
## [1] 23 24 25

7.1 Name List Element in R

names(sample_list)<-c("Name","Roll_Num")

sample_list
## $Name
## [1] "Manish" "Amit"   "Amul"  
## 
## $Roll_Num
## [1] 23 24 25

7.2 Accessing Elements of List

To access first element of list use :-

sample_list[1]
## $Name
## [1] "Manish" "Amit"   "Amul"

You can also access list elements using list names

sample_list$Name
## [1] "Manish" "Amit"   "Amul"
sample_list$Roll_Num
## [1] 23 24 25

You can also access list elements by name

sample_list[1][1]
## $Name
## [1] "Manish" "Amit"   "Amul"