Two Dimensional List in R Programming

10 May, 2020
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.
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 listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8))  # create a list_data with two lists as its elementslist_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.
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 listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8))  # create a list_data with two lists as its elementslist_data <- list(list1, list2)  # runs uptil the length of outer listfor (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
Deletion or modification of inner listInner 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.
Deleting or Updating elements of inner listsElements 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 ListsThe following R code is used for the modification of lists:# list1 and list2 are uni-dimensional listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8)) # create a list_data with two lists as its elementslist_data <- list(list1, list2)print ("The original list is : ")print (list_data) # modifying third component of first listlist_data[[1]][[3]] = "you"print ("Modification 1")print (list_data) # modifying second inner listlist_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 3Deletion of ListsThe following R code is used for the deletion of lists:# list1 and list2 are uni-dimensional listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8)) # create a list_data with two lists as its elementslist_data <- list(list1, list2)print ("The original list is : ")print (list_data) # deleting third component of first listlist_data[[1]][[3]] = NULLprint ("Modification 1")print (list_data) # deleting second inner listlist_data[[2]] <- NULLprint ("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.
Modification of ListsThe following R code is used for the modification of lists:
# list1 and list2 are uni-dimensional listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8)) # create a list_data with two lists as its elementslist_data <- list(list1, list2)print ("The original list is : ")print (list_data) # modifying third component of first listlist_data[[1]][[3]] = "you"print ("Modification 1")print (list_data) # modifying second inner listlist_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 ListsThe following R code is used for the deletion of lists:
# list1 and list2 are uni-dimensional listslist1 <- list (c(1:5), "hi", 0 + 5i)list2 <- list(c(6:8)) # create a list_data with two lists as its elementslist_data <- list(list1, list2)print ("The original list is : ")print (list_data) # deleting third component of first listlist_data[[1]][[3]] = NULLprint ("Modification 1")print (list_data) # deleting second inner listlist_data[[2]] <- NULLprint ("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.
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
Printing Output of an R Program
Group by function in R using Dplyr
How to Replace specific values in column in R DataFrame ?
How to filter R DataFrame by values in a column?
How to change Row Names of DataFrame in R ?
Creating a Data Frame from Vectors in R Programming
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R