Open In App

Two Dimensional List in R Programming

Improve
Improve
Like Article
Like
Save
Share
Report

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 until 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 until the length of outer list
for (i in c(1 : length(list_data)))
{
    # runs until 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

  • Deletion or modification of inner list
    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.
  • Deleting or Updating elements of inner lists
    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.



  • Last Updated : 07 Dec, 2022
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads