如何在R中组合数据框列表?
如果我们有一个数据框列表并且这些数据框的大小相同,那么我们可能想要组合这些列表,以便可以组合数据框。这可以通过使用mapply函数和cbind来完成。例如,如果我们有两个定义为List1和List2的数据框列表,那么我们可以使用以下命令将它们组合起来-
mapply(cbind,List1,List2,SIMPLIFY=FALSE).
示例
考虑以下数据框-
> x1<-rnorm(10) > x2<-rnorm(10) > df1<-data.frame(x1,x2) > df1输出结果
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
示例
> y1<-rnorm(10) > y2<-rnorm(10) > df2<-data.frame(y1,y2) > df2输出结果
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
示例
> List1<-list(df1,df2) > List1输出结果
[[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
列表2
示例
> a1<-rnorm(10) > a2<-rnorm(10) > df3<-data.frame(a1,a2) > df3输出结果
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
示例
> b1<-rnorm(10) > b2<-rnorm(10) > df4<-data.frame(b1,b2) > df4输出结果
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
示例
> List2<-list(df3,df4) > List2输出结果
[[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
结合数据框列表-
示例
> mapply(cbind,List1,List2,SIMPLIFY=FALSE)输出结果
[[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