Page 227 - Data Science class 11
P. 227
On execution of the above code, the following result is shown:
6.8.3 manipulate the r list elements
In R, we can add, delete, or update elements of the list. The addition or deletion of the elements can only be done at
the end of the list. However, an update can be performed on any element in the list.
Let’s now discuss how to manipulate the R list elements with the help of an example.
Example
Enter the following code snippet:
#Creating a list containing a vector, a matrix and a list.
data_list <- list(c("Jan","Feb","Mar"),
matrix(c(1,2,3,4,-1,9), nrow = 2),list("Red",12.3))
#Give names to the elements in the list.
names(data_list) <- c("Month", "Matrix", "Misc")
#Add an element at the end of the list.
data_list[4] <- "New element"
print(data_list[4])
#Remove the last element.
data_list[4] <- NULL
print(data_list[4])
#Update the 3rd Element.
data_list[3] <- "updated element"
Programming with R 225

