Two Dimensional List in R Programming
Last Updated :
07 Dec, 2022
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 [[ ]].
Python3 1==
# 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:
Python3 1==
# 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:
Python3 1==
# 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:
Python3 1==
# 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.
Similar Reads
Plotting Graphs using Two Dimensional List in R Programming
List is a type of an object in R programming. Lists can contain heterogeneous elements like strings, numeric, matrices, or even lists. A list is a generic vector containing other objects. Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nest
2 min read
How to Code in R programming?
R is a powerful programming language and environment for statistical computing and graphics. Whether you're a data scientist, statistician, researcher, or enthusiast, learning R programming opens up a world of possibilities for data analysis, visualization, and modeling. This comprehensive guide aim
4 min read
Assigning Vectors in R Programming
Vectors are one of the most basic data structure in R. They contain data of same type. Vectors in R is equivalent to arrays in other programming languages. In R, array is a vector of one or more dimensions and every single object created is stored in the form of a vector. The members of a vector are
5 min read
Hello World in R Programming
When we start to learn any programming languages we do follow a tradition to begin HelloWorld as our first basic program. Here we are going to learn that tradition. An interesting thing about R programming is that we can get our things done with very little code. Before we start to learn to code, le
2 min read
Functions in R Programming
A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you a
8 min read
Environments in R Programming
The environment is a virtual space that is triggered when an interpreter of a programming language is launched. Simply, the environment is a collection of all the objects, variables, and functions. Or, Environment can be assumed as a top-level object that contains the set of names/variables associat
3 min read
File Handling in R Programming
In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the worki
4 min read
Writing to Files in R Programming
R programming Language is one of the very powerful languages specially used for data analytics in various fields. Analysis of data means reading and writing data from various files like excel, CSV, text files, etc. Today we will be dealing with various ways of writing data to different types of file
2 min read
String Concatenation in R Programming
String concatenation is a way of appending two or more strings into a single string whether it is character by character or using some special character end to end. There are many ways to perform string concatenation. Example: Input: str1 = 'Geeks' str2 = 'for' str3 = 'Geeks' Output: 'GeeksforGeeks
3 min read
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read