DataFrames are generic data objects of R which are used to store the tabular data. They are two-dimensional, heterogeneous data structures. A list in R, however, comprises of elements, vectors, data frames, variables, or lists that may belong to different data types. In this article, we will study how to create a list consisting of data frames as its components and how to access, modify, and delete these data frames to lists. list() function in R creates a list of the specified arguments. The data frames specified as arguments in this function may have different lengths.
Operations that can be performed on a list of DataFrames are:
- Creating a list of Dataframes
- Accessing components of a list of Dataframes
- Modifying components of a list of Dataframes
- Concatenation of lists of Dataframes
- Deleting components of a list of Dataframes
Creating a list of Dataframes
To create a list of Dataframes we use the list() function in R and then pass each of the data frame you have created as arguments to the function.
Example:
Python3
# R program to create list of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Create list of data frame using list()
listOfDataframe = list(df1, df2)
print(listOfDataframe)
Output:
[[1]]
y1 y2
1 1 4
2 2 5
3 3 6
[[2]]
y1 y2
1 7 1
2 8 4
3 9 6
Accessing components of a list of Dataframes
We can access components of a list of data frames in two ways.
- Access components by names: All the components of a list of data frames can be named and we can use those names to access the components of the list using the dollar command.
Example:
Python3
# R program to access components
# of a list of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
"Dataframe1" = df1,
"Dataframe2" = df2
)
print(listOfDataframe)
# Accessing components by names
cat("Accessing Dataframe2 using $ command\n")
print(listOfDataframe$Dataframe2)
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
Accessing Dataframe2 using $ command
y1 y2
1 7 1
2 8 4
3 9 6
- Access components by indices: We can also access the components of the list of data frames using indices. To access the top-level components of a list of data frames we have to use a double slicing operator “[[ ]]” which is two square brackets and if we want to access the lower or inner level components of a list we have to use another square bracket “[ ]” along with the double slicing operator “[[ ]]”.
Example:
Python3
# R program to access components
# of a list of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
"Dataframe1" = df1,
"Dataframe2" = df2
)
print(listOfDataframe)
# Accessing a top level components by indices
cat("Accessing Dataframe2 using indices\n")
print(listOfDataframe[[2]])
# Accessing a inner level components by indices
cat("Accessing second column from Dataframe1 using indices\n")
print(listOfDataframe[[1]][2])
# Accessing another inner level components by indices
cat("Accessing 4 from Dataframe2 using indices\n")
# Here [2, 2] represents that I want
# to access element from second row and second column i.e 4 here
print(listOfDataframe[[2]][2, 2])
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
Accessing Dataframe2 using indices
y1 y2
1 7 1
2 8 4
3 9 6
Accessing second column from Dataframe1 using indices
y2
1 4
2 5
3 6
Accessing 4 from Dataframe2 using indices
[1] 4
Modifying components of a list of Dataframes
A list of data frames can also be modified by accessing the components and replacing them with the ones which you want.
Example:
Python3
# R program to modify components
# of a list of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
"Dataframe1" = df1,
"Dataframe2" = df2
)
cat("Before modifying the list of data frame\n")
print(listOfDataframe)
# Modifying the dataframe2
listOfDataframe$Dataframe2 = data.frame(
y1 = c(70, 80, 9),
y2 = c(14, 41, 63)
)
# Modifying second column from Dataframe1
listOfDataframe[[1]][2] = c(23, 45, 67)
# Modifying element 2 from dataframe1
listOfDataframe[[1]][2, 1] = 15
cat("After modified the list of data frame\n")
print(listOfDataframe)
Output:
Before modifying the list of data frame
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
After modified the list of data frame
$Dataframe1
y1 y2
1 1 23
2 15 45
3 3 67
$Dataframe2
y1 y2
1 70 14
2 80 41
3 9 63
Concatenation of lists of Dataframes
Two lists of data frames can be concatenated using the concatenation function. So, when we want to concatenate two lists of data frames we have to use the concatenation operator.
Syntax:
list = c(list, list1)
list = the original list of the data frame
list1 = the new list of the data frame
Example:
Python3
# R program concatenation
# of lists of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
"Dataframe1" = df1,
"Dataframe2" = df2
)
cat("Before concatenation of the new list of data frame\n")
print(listOfDataframe)
# Creating another one list of data frame
df3 = data.frame(
y1 = c(7, 8, 98),
y2 = c(10, 44, 6)
)
newListOfDataframe = list(
"Dataframe3" = df3
)
# Concatenation of list of data frames
# using concatenation operator
listOfDataframe = c(listOfDataframe, newListOfDataframe)
cat("After concatenation of the new list of data frame\n")
print(listOfDataframe)
Output:
Before concatenation of the new list of data frame
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
After concatenation of the new list of data frame
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
$Dataframe3
y1 y2
1 7 10
2 8 44
3 98 6
Deleting components from a list of Dataframes
To delete components of a list of data frames, first of all, we need to access those components and then insert a negative sign before those components. It indicates that we had to delete that component.
Example:
Python3
# R program to delete components
# of a list of data frames
# Create dataframe
df1 = data.frame(
y1 = c(1, 2, 3),
y2 = c(4, 5, 6)
)
# Create another dataframe
df2 = data.frame(
y1 = c(7, 8, 9),
y2 = c(1, 4, 6)
)
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
"Dataframe1" = df1,
"Dataframe2" = df2
)
cat("Before deletion the list is\n")
print(listOfDataframe)
# Deleting a top level components
cat("After Deleting Dataframe1\n")
print(listOfDataframe[[-1]])
# Deleting a inner level components
cat("After Deleting first column from Dataframe2\n")
print(listOfDataframe[[2]][-1])
Output:
Before deletion the list is
$Dataframe1
y1 y2
1 1 4
2 2 5
3 3 6
$Dataframe2
y1 y2
1 7 1
2 8 4
3 9 6
After Deleting Dataframe1
y1 y2
1 7 1
2 8 4
3 9 6
After Deleting first column from Dataframe2
y2
1 1
2 4
3 6
Similar Reads
Plot columns from list of dataframes in R
In this article, we will discuss how to plot columns from a list of dataframes in R programming language. Note: We are taking a line plot for implementation same can be applied to any other plot. The ggplot() method is supplemented by different geometrical shapes to indicate the type of data plotti
2 min read
DataFrame Operations in R
DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as mattresses where each column of
9 min read
DataFrame Row Slice in R
In this article, we are going to see how to Slice row in Dataframe using R Programming Language. Row slicing in R is a way to access the data frame rows and further use them for operations or methods. The rows can be accessed in any possible order and stored in other vectors or matrices as well. Row
4 min read
Matrix vs Dataframe in R
A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks.The two most important data structures in R are Matrix and Dataframe, they look the same but different in nature.  Matri
3 min read
R - DataFrame Manipulation
Data Frame is a two-dimensional structured entity consisting of rows and columns. It consists equal length vectors as rows. The data is stored in cells which are accessed by specifying the corresponding [row, col] set of values of the data frame. Manipulation of data frames involve modifying, extrac
9 min read
Convert dataframe to nested list in R
In R Programming Data frame is nothing but a two-dimensional entity that consists of rows and columns whereas Lists in R are data structures that are able to store multiple data values at once. List Creation Lists in R can be created using list() function. Syntax: list(element1, element2, element3,.
2 min read
How to find length of data frame in R
In this article, we will see What is a Data Frame and how to find the length of a data frame in R programming Language. Return the length (total number of rows) of the Data Frame using nrow()nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will
3 min read
Intersection of dataframes using Dplyr in R
In this article, we will discuss how to find the Intersection of two dataframes using the Dplyr package in R programming language. Dplyr provides intersect() method to get the common data in two dataframes. Syntax: intersect(dataframe1,dataframe2,dataframe3,........,dataframe n) We can perform this
1 min read
How to Split Vector and DataFrame in R
R is a programming language and environment specifically designed for facts analysis, statistical computing, and graphics. Sometimes it is required to split data into batches for various data manipulation and analysis tasks. In this article, we will discuss some techniques to split vectors into chun
6 min read
Find last occurrence of a data frame in R
In this article, we will explore various methods to find the last occurrence in a data frame by using the R Programming Language. R language offers various methods to find the last occurrence in a data frame. By using these methods provided by R, it is possible to find every occurrence. Some of the
3 min read