A list in R is basically an R object that contains within it, elements belonging to different data types, which may be numbers strings or even other lists. Basically, a list can contain other objects which may be of varying lengths. The list is defined using the list() function in R.
A two-dimensional list can be considered as a “list of lists”. A two-dimensional list can be considered as a matrix where each row can have different lengths and supports different data types.
Creating two-dimensional Lists
One-dimensional lists can be first created using list() function. They can be further enclosed into another outer list. The length of the outer list is the number of inner lists it contains, which is accessed by length() function. The length of the various inner lists can be computed by indexing by using length (list[[index]]) function, where the corresponding index is accessed by [[ ]].
# list1 and list2 are uni-dimensional lists list1 <- list (c(1:5), "hi", 0 + 5i) list2 <- list(c(6:8)) # create a list_data with two lists as its elements list_data <- list(list1, list2) print ("The two-dimensional list is : ") print (list_data) cat("Length of nested list is : ", length (list_data)) cat("Length of first inner list is : ", length (list_data[[1]])) |
Output:
[1] "The two-dimensional list is : " [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[1]][[3]] [1] 0+5i [[2]] [[2]][[1]] [1] 6 7 8 Length of nested list is : 2 Length of first inner list is : 3
The first list contains three elements, of varying sizes and data types, a sequence of numbers 1 to 5, a string, and a complex number. The second list contains a vector of length three consisting of numbers 6 to 8.
Accessing nested lists
All the elements of the list can be accessed by a nested for loop. The outer loop runs uptil the number of elements of the outer list. The inner loop comprises of the individual lengths of the inner lists.
The following R code indicates working with two-dimensional lists:
# list1 and list2 are uni-dimensional lists list1 <- list (c(1:5), "hi", 0 + 5i) list2 <- list(c(6:8)) # create a list_data with two lists as its elements list_data <- list(list1, list2) # runs uptil the length of outer list for (i in c(1 : length(list_data))) { # runs uptil the length of inner lists at ith indices for (j in c(1: length(list_data[[i]]))) { cat ("List", i, "element", j, ": ") print (list_data[[i]][[j]]) } } |
Output:
List 1 element 1 : [1] 1 2 3 4 5 List 1 element 2 : [1] "hi" List 1 element 3 : [1] 0+5i List 2 element 1 : [1] 6 7 8
Deleting or Updating elements
Inner lists can be modified by a single level of indexing. The corresponding inner list element is set to a new value. If the new value is equal to NULL, the element is deleted otherwise modified.
Elements of inner lists can be deleted or modified by double level of indexing. The element to be modified is set to a new value. If the value is NULL, the corresponding element is deleted. Otherwise, modified.
Modification of Lists
The following R code is used for the modification of lists:
# list1 and list2 are uni-dimensional lists list1 <- list (c(1:5), "hi", 0 + 5i) list2 <- list(c(6:8)) # create a list_data with two lists as its elements list_data <- list(list1, list2) print ("The original list is : ") print (list_data) # modifying third component of first list list_data[[1]][[3]] = "you"print ("Modification 1") print (list_data) # modifying second inner list list_data[[2]] <- list (c(0:3)) print ("Modification 2") print (list_data) |
Output:
[1] "The original list is : " [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[1]][[3]] [1] 0+5i [[2]] [[2]][[1]] [1] 6 7 8 [1] "Modification 1" [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[1]][[3]] [1] “you” [[2]] [[2]][[1]] [1] 6 7 8 [1] "Modification 2" [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[1]][[3]] [1] "you" [[2]] [[2]][[1]] [1] 0 1 2 3
Deletion of Lists
The following R code is used for the deletion of lists:
# list1 and list2 are uni-dimensional lists list1 <- list (c(1:5), "hi", 0 + 5i) list2 <- list(c(6:8)) # create a list_data with two lists as its elements list_data <- list(list1, list2) print ("The original list is : ") print (list_data) # deleting third component of first list list_data[[1]][[3]] = NULL print ("Modification 1") print (list_data) # deleting second inner list list_data[[2]] <- NULL print ("Modification 2") print (list_data) |
Output:
[1] "The original list is : " [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[1]][[3]] [1] 0+5i [[2]] [[2]][[1]] [1] 6 7 8 [1] "Modification 1" [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi" [[2]] [[2]][[1]] [1] 6 7 8 [1] "Modification 2" [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] "hi"
After modification 1, the size of the inner list one reduces by one. After modification 2, the second inner list is deleted and the size of the outer list reduces by one.
Recommended Posts:
- Plotting Graphs using Two Dimensional List in R Programming
- Create One Dimensional Scatterplots in R Programming - stripchart() Function
- Convert an Object to List in R Programming - as.list() Function
- Check if the Object is a List in R Programming - is.list() Function
- Apply a Function over a List of elements in R Programming - lapply() Function
- Get a List of all the Attached Packages in R Programming - search() Function
- Get the List of Arguments of a Function in R Programming - args() Function
- Recursively apply a Function to a List in R Programming - rapply() function
- Named List in R Programming
- List all the Objects present in the Current Working Directory in R Programming - ls() Function
- Coercing an Object of mode "list" to mode "call" in R Programming - as.call() Function
- Get a List of Numbers in the Specified Range in R Programming - seq.int() Function
- Get a List of all the 657 colors in R Programming - colors() Function
- Get a List of points obtained by Interpolation in R Programming - spline() and splinefun() Function
- Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
- Intersection of Two Objects in R Programming - intersect() Function
- Check if Two Objects are Equal in R Programming - setequal() Function
- Concatenate Two Strings in R programming - paste() method
- Union of two Objects in R Programming - union() Function
- Performing different Operations on Two Arrays in R Programming - outer() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

