List of Dataframes in R
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) |
- Output:
$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 ]) |
- Output:
$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
Contact Us