Lists are objects that contain elements of different types, such as:
These can be created by using the list()
function.
Note that these attributes attr1
and attr2
are the components of data structures such as numbers, strings, vectors, etc.
my_list <- list(attr1, attr2 ...)
This is an example of how to create a list that contains elements like strings, numbers, vectors, and a logical value.
The code below creates a list and prints each element of the list with the corresponding index.
#Create a list contain strings, numbers, vectors and a logical valuesList_data <- list("Red", "Green", c(21,32, 11), TRUE, 51.23, 119.1)print(List_data)
We can modify a component in a list through reassignment by accessing a specific index and assigning this index with another new value.
//modifying the results
List_data[["Red"]] <- "Blue";
Adding a new component to the list is simple. Here is the basic syntax:
// adding new value
List_data[["White"]] <- FALSE
Assigning FALSE
to a specific index or value, it results in deletion.
//
List_data[["Red"]] <- NULL