Open In App

How to extract certain columns from a list of dataframe in R?

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A nested list may contain other lists, or various objects, arrays, vectors or dataframes as its components. Each component is mapped to a unique index position in the R programming language. Any element can be easily indexed using the [[]] operator, which is used for referencing the particular indexes of the list. Subsequently, list_name[[ index ]], will get the component stored at the index position. In this scenario, this method returns the dataframe at the index position. 

The particular column of the extracted dataframe can then be captured using df$col-name or df[[col-index]] , which fetches the mentioned column in the form of a vector. The mentioned column should be well within the bounds, otherwise, an error is thrown. By default, the indexes start with 1. 

Syntax:

df [[ col-index]] OR df$col-name

Example 1:

R




# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4],
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
 
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9),
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
 
combined_df <- list(data_frame1,data_frame2)
print ("Dataframe2 Column1")
col21 <- combined_df[[2]][[1]]
print (col21)


Output

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "Dataframe2 Column1"
[1] 6 7 8 9

The list() method may contain more than two components that can be stacked together. The column of the dataframe may be a single element or a vector of atomic elements. 

Example 2:

R




# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4],
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
 
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9),
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
 
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
 
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe3 Column2")
col32 <- combined_df[[3]][[2]]
print (col32)


Output

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "DataFrame3"
    C1 C2
1 FALSE  1
[1] "Dataframe3 Column2"
[1] 1

The column name can also be referenced using the $ operator, which is used as an indexing operator in R. 

Example 3:

R




# declaring a dataframe
data_frame1 = data.frame(col1 = letters[1:4],
                         col2 = c(5:8) , col3 = TRUE)
print ("DataFrame1")
print (data_frame1)
 
# declaring a dataframe
data_frame2 = data.frame(col1 = c(6:9),
                         col2 = LETTERS[1:4])
print ("DataFrame2")
print (data_frame2)
 
# declaring a dataframe
data_frame3 = data.frame(C1 = FALSE, C2 = 1)
print ("DataFrame3")
print (data_frame3)
 
combined_df <- list(data_frame1,data_frame2,data_frame3)
print ("Dataframe1 Column1")
col11 <- combined_df[[1]]$col1
print (col11)


Output

[1] "DataFrame1"
 col1 col2 col3
1    a    5 TRUE
2    b    6 TRUE
3    c    7 TRUE
4    d    8 TRUE
[1] "DataFrame2"
 col1 col2
1    6    A
2    7    B
3    8    C
4    9    D
[1] "DataFrame3"
    C1 C2
1 FALSE  1
[1] "Dataframe1 Column1"
[1] a b c d
Levels: a b c d


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads