How to combine lists of data frames in R?

R ProgrammingServer Side ProgrammingProgramming

If we have a list of data frames and the size of those data frames is same then we might want to combine the lists so that the data frames can be combined. This can be done by using mapply function along with cbind. For example, if we have two lists of data frames defined as List1 and List2 then we can combine them using the command −

mapply(cbind,List1,List2,SIMPLIFY=FALSE).

Example

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

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

Combining the list of data frames −

Example

> mapply(cbind,List1,List2,SIMPLIFY=FALSE)

Output

[[1]]
x1 x2 a1 a2
1 0.2378371 0.51433808 1.5711728 0.2861241
2 0.0638975 -1.66077353 0.8062374 0.9469154
3 0.3987209 0.68480587 1.1505496 -0.5894829
4 -1.1321073 0.29528261 0.9164866 -0.3137043
5 -0.5603269 1.14556819 -1.3424446 -1.2921698
6 2.2072545 -1.20718355 -0.1499540 -0.8940665
7 0.8196423 0.38380242 -0.1498557 -1.1361156
8 -2.2394064 0.06741712 0.9299988 0.7679135
9 -0.7356725 -1.46968026 -1.7079005 -0.7099908
10 -1.4642820 -1.39423679 0.8146867 1.3921303

[[2]]
y1 y2 b1 b2
1 2.2307515 0.375538934 -1.7113866 1.7014637
2 -1.3539616 -0.169574915 -0.0202485 1.2428109
3 -0.1332480 -0.788416414 -0.3892979 -1.5831333
4 1.3181498 1.887995737 0.2127277 -0.4943695
5 -1.4384012 1.261034365 -0.4846616 1.0283278
6 0.3725585 -0.493219141 -1.4116239 -1.4882983
7 -0.7806511 -1.177616450 -0.1737286 -0.1101114
8 -0.4772392 0.250962895 1.4613389 0.1531942
9 -0.8932982 -0.004567268 -0.1573986 0.3431330
10 0.2224190 -0.203232106 -0.2782074 0.5439397
raja
Updated on 02-Jan-2021 10:03:42

Advertisements