How to extract the column from a list of data frames in R?



In Data Analysis, we often required to extract a single value, a single row, or a single column for a specific analysis. For example, if data frame contains column defined as height and weight then we might want to use only height then it can be extracted, this could be a part of a list as well, therefore, extraction from list will be required. If we have a list of data frames then extraction of a column from one of the data frames in the list can be done by using double square brackets for accessing the data frame and the column number as shown in the below examples.

Example1

Consider the below data frame −

Live Demo

> x1<-rnorm(10) > x2<-rnorm(10) > df1<-data.frame(x1,x2) > df1

Output

 x1 x2 1 0.2378371 0.51433808 2 0.0638975 -1.66077353 3 0.3987209 0.68480587 4 -1.1321073 0.29528261 5 -0.5603269 1.14556819 6 2.2072545 -1.20718355 7 0.8196423 0.38380242 8 -2.2394064 0.06741712 9 -0.7356725 -1.46968026 10 -1.4642820 -1.39423679

Example

Live Demo

> y1<-rnorm(10) > y2<-rnorm(10) > df2<-data.frame(y1,y2) > df2

Output

 y1 y2 1 2.2307515 0.375538934 2 -1.3539616 -0.169574915 3 -0.1332480 -0.788416414 4 1.3181498 1.887995737 5 -1.4384012 1.261034365 6 0.3725585 -0.493219141 7 -0.7806511 -1.177616450 8 -0.4772392 0.250962895 9 -0.8932982 -0.004567268 10 0.2224190 -0.203232106

Example

> List1<-list(df1,df2) > List1

Output

[[1]] x1 x2 1 0.2378371 0.51433808 2 0.0638975 -1.66077353 3 0.3987209 0.68480587 4 -1.1321073 0.29528261 5 -0.5603269 1.14556819 6 2.2072545 -1.20718355 7 0.8196423 0.38380242 8 -2.2394064 0.06741712 9 -0.7356725 -1.46968026 10 -1.4642820 -1.39423679 [[2]] y1 y2 1 2.2307515 0.375538934 2 -1.3539616 -0.169574915 3 -0.1332480 -0.788416414 4 1.3181498 1.887995737 5 -1.4384012 1.261034365 6 0.3725585 -0.493219141 7 -0.7806511 -1.177616450 8 -0.4772392 0.250962895 9 -0.8932982 -0.004567268 10 0.2224190 -0.203232106

Extracting column 2 from data frame 2 in List1 −

Example

Live Demo

> List1[[2]][[2]]

Output

[1] 0.375538934 -0.169574915 -0.788416414 1.887995737 1.261034365 [6] -0.493219141 -1.177616450 0.250962895 -0.004567268 -0.203232106

List2

Example

Live Demo

> a1<-rnorm(10) > a2<-rnorm(10) > df3<-data.frame(a1,a2) > df3

Output

 a1 a2 1 1.5711728 0.2861241 2 0.8062374 0.9469154 3 1.1505496 -0.5894829 4 0.9164866 -0.3137043 5 -1.3424446 -1.2921698 6 -0.1499540 -0.8940665 7 -0.1498557 -1.1361156 8 0.9299988 0.7679135 9 -1.7079005 -0.7099908 10 0.8146867 1.3921303

Example

Live Demo

> b1<-rnorm(10) > b2<-rnorm(10) > df4<-data.frame(b1,b2) > df4

Output

 b1 b2 1 -1.7113866 1.7014637 2 -0.0202485 1.2428109 3 -0.3892979 -1.5831333 4 0.2127277 -0.4943695 5 -0.4846616 1.0283278 6 -1.4116239 -1.4882983 7 -0.1737286 -0.1101114 8 1.4613389 0.1531942 9 -0.1573986 0.3431330 10 -0.2782074 0.5439397

Example

> List2<-list(df3,df4) > List2

Output

[[1]] a1 a2 1 1.5711728 0.2861241 2 0.8062374 0.9469154 3 1.1505496 -0.5894829 4 0.9164866 -0.3137043 5 -1.3424446 -1.2921698 6 -0.1499540 -0.8940665 7 -0.1498557 -1.1361156 8 0.9299988 0.7679135 9 -1.7079005 -0.7099908 10 0.8146867 1.3921303 [[2]] b1 b2 1 -1.7113866 1.7014637 2 -0.0202485 1.2428109 3 -0.3892979 -1.5831333 4 0.2127277 -0.4943695 5 -0.4846616 1.0283278 6 -1.4116239 -1.4882983 7 -0.1737286 -0.1101114 8 1.4613389 0.1531942 9 -0.1573986 0.3431330 10 -0.2782074 0.5439397 

Extracting column 2 from data frame 1 in List2 −

Example

> List2[[1]][[2]]

Output

[1] 0.2861241 0.9469154 -0.5894829 -0.3137043 -1.2921698 -0.8940665 [7] -1.1361156 0.7679135 -0.7099908 1.3921303
Updated on: 2021-01-02T10:19:01+05:30

872 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements