To understand the list concept,creating,accesing and manipulating.
List creation :
Lists is a collection of elements of different types like numeric,character,matrix,vector,a list and also functions.
Created using function list()
Naming list elements .:
Syntax :
names(listname)=c(names for the elements)
Accessing list elements :
The elements of the list can be accessed by using their names or by indexing
Manipulating elements :
Merging many into one list can be done by placing all the list inside one list.
Converting list to a vector :
A list can be converted to a vector so that it can be taken to further manipulation.To achieve this the function unlist() is used.
#Creating a list
a=list(20,45,67,c(“aaa”,”bbb”,”ccc”),matrix(c(1,2,3,4,5,6),nrow=3),list(10,20,30))
print(a)
print(class(a))
#naming the list elements
names(a)<-c(“x1″,”x2″,”x3″,”vector”,”matrix”,”Inner list”)
print(a)
#accessing list elements
cat(“The fourth element is “,”\n”)
print(a[4])
cat(“Accessing the fifth element by name “,”\n”)
print(a$matrix)
#manipulating list element
a[7]=”New element”
cat(“New element added successfully\n”)
print(a[7])
a[7]=NULL
cat(“Last element deleted\n”)
a[5]=”updated element”
print(a[5])
#merging list
b=list(23,”aaa”,89.5,TRUE,list(1,2,3))
c=list(5,66.7,TRUE,FALSE)
d=c(b,c)
print(d)
print(class(d))
#converting list to a vector
e=unlist(a)
print(class(a))