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

13 Dec, 2021
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 dataframedata_frame1 = data.frame(col1 = letters[1:4],                         col2 = c(5:8) , col3 = TRUE)print ("DataFrame1")print (data_frame1) # declaring a dataframedata_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 dataframedata_frame1 = data.frame(col1 = letters[1:4],                         col2 = c(5:8) , col3 = TRUE)print ("DataFrame1")print (data_frame1) # declaring a dataframedata_frame2 = data.frame(col1 = c(6:9),                         col2 = LETTERS[1:4])print ("DataFrame2")print (data_frame2) # declaring a dataframedata_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 dataframedata_frame1 = data.frame(col1 = letters[1:4],                         col2 = c(5:8) , col3 = TRUE)print ("DataFrame1")print (data_frame1) # declaring a dataframedata_frame2 = data.frame(col1 = c(6:9),                         col2 = LETTERS[1:4])print ("DataFrame2")print (data_frame2) # declaring a dataframedata_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]]$col1print (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
nnr223442
Picked
R DataFrame-Programs
R List-Programs
R-DataFrame
R-List
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?