How to create a single data frame from data frames stored in a list with row names in R?

R ProgrammingServer Side ProgrammingProgramming

If we have multiples data frames of same size stored in a list and we believe that these data frames have similar characteristic then we can create a single data frame. This can be done with the help of do.call. For example, if we have a list defined with the name List containing data frames with equal number of rows with their names then a single data frame can be created do.call(rbind,unname(List)).

Example

 Live Demo

df1<−data.frame(x=rnorm(10),row.names=sample(LETTERS[1:26],10))
df2<−data.frame(x=rnorm(10),row.names=sample(LETTERS[1:26],10))
df3<−data.frame(x=rnorm(10),row.names=sample(LETTERS[1:26],10))
List<−list(First=df1,Second=df2,Third=df3)
List

Output

$First
x
G 0.30699620
P 1.11451394
I 0.16537658
J −0.99267571
X −0.00183875
T 0.65413132
M 0.46023584
H −0.23309683
W 0.71764386
L 0.71318867
$Second
x
B 0.12553866
X 0.78585517
D −0.86144038
I −0.46069668
T −0.04954584
S 0.41792421
L 0.77762054
U 0.10214920
P −0.54777071
Z −0.17795518
$Third
x
P −1.03833288
A −2.06108259
D 0.06075337
S −0.25815094
V −1.14154154
Z 1.28649520
R 0.58446876
O −0.08794448
C −0.14094378
N −0.49827426

Converting list data frames into a single data frame −

Example

df<−do.call(rbind,unname(List))
df

Output

x
G 0.30699620
P 1.11451394
I 0.16537658
J −0.99267571
X −0.00183875
T 0.65413132
M 0.46023584
H −0.23309683
W 0.71764386
L 0.71318867
B 0.12553866
X1 0.78585517
D −0.86144038
I1 −0.46069668
T1 −0.04954584
S 0.41792421
L1 0.77762054
U 0.10214920
P1 −0.54777071
Z −0.17795518
P2 −1.03833288
A −2.06108259
D1 0.06075337
S1 −0.25815094
V −1.14154154
Z1 1.28649520
R 0.58446876
O −0.08794448
C −0.14094378
N −0.49827426
raja
Published on 08-Feb-2021 11:32:48
Advertisements