Page 226 - Data Science class 11
P. 226
data_list <- list(c("Jan","Feb","Mar","April"), matrix(c(1,2,3,4,-1,9), nrow =
3),list("Red",12.3))
names(data_list) <- c("Month", "Matrix", "Misc")
print(data_list)
On execution of the above code, the following result is shown:
6.8.2 accessing List elements
Let’s now understand how to access list elements in R programming. You will be using the list that you created in the
previous section.
Example
Enter the following code snippet:
#In order to give names to the elements of the list:
names(data_list) <- c("Month", "Matrix", "Misc")
#Access the first element of the list.
print(data_list[1]) #Accessing the First element
#Access the third element.
print(data_list[3]) #Accessing the Third element
By using the name of the element access the list elements.
Enter the following code snippet:
print(data_list$Matrix) #Using name of access element
names(data_list) <- c("Month", "Matrix", "Misc")
names(data_list) <- c("Month", "Matrix", "Misc")
print(data_list[1]) #Accessing the First element
print(data_list[3]) #Accessing the Third element
print(data_list$Matrix) #Using name of access element
names(data_list) <- c("Month", "Matrix", "Misc")
224 Touchpad Data Science-XI

