May 12, 2021 R language tutorial
A list is an R-language object that contains different types of elements, such as numbers, strings, vectors, and another list there. A list can also contain a matrix or function as its element. T he list was created using the list() function.
The following is an example of creating a list of strings, numbers, vectors, and logical values
# Create a list containing strings, numbers, vectors and a logical values. list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1) print(list_data)
When we execute the code above, it produces the following results -
[[1]] [1] "Red" [[2]] [1] "Green" [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 119.1
List elements can give names, and you can use those names to access them.
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Show the list. print(list_data)
When we execute the code above, it produces the following results -
$`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Matrix [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8 $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3
The elements of the list can be accessed through the index of the elements in the list. I n the case of named lists, it can also be accessed by name.
Let's continue to use the list of examples above -
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Access the first element of the list. print(list_data[1]) # Access the thrid element. As it is also a list, all its elements will be printed. print(list_data[3]) # Access the list element using the name of the element. print(list_data$A_Matrix)
When we execute the code above, it produces the following results -
$`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3 [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8
We can add, remove, and update list elements as shown below. W e can only add and remove elements at the end of the list. B ut we can update any element.
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Add element at the end of the list. list_data[4] <- "New element" print(list_data[4]) # Remove the last element. list_data[4] <- NULL # Print the 4th Element. print(list_data[4]) # Update the 3rd Element. list_data[3] <- "updated element" print(list_data[3])
When we execute the code above, it produces the following results -
[[1]] [1] "New element" $ NULL $`A Inner list` [1] "updated element"
By putting all the lists in one list() function, you can combine many lists into one list.
# Create two lists. list1 <- list(1,2,3) list2 <- list("Sun","Mon","Tue") # Merge the two lists. merged.list <- c(list1,list2) # Print the merged list. print(merged.list)
When we execute the code above, it produces the following results -
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] "Sun" [[5]] [1] "Mon" [[6]] [1] "Tue"
Lists can be converted to vectors so that elements of vectors can be used for further operations. Y ou can apply all arithmetic operations to vectors after converting the list to vectors. T o do this conversion, we use the unlist() function. I t takes the list as input and generates vectors.
# Create lists. list1 <- list(1:5) print(list1) list2 <-list(10:14) print(list2) # Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) # Now add the vectors result <- v1+v2 print(result)
When we execute the code above, it produces the following results -
[[1]] [1] 1 2 3 4 5 [[1]] [1] 10 11 12 13 14 [1] 1 2 3 4 5 [1] 10 11 12 13 14 [1] 11 13 15 17 19