2013-12-16 15 views
9

Ich habe, was kein Zweifel ist ein einfaches Problem. Ich habe die letzte Stunde nach einer Lösung gesucht, aber mir fehlt eindeutig etwas. Wenn dies tatsächlich ein Duplikat bitte Link mich auf den richtigen Weg, dies zu tun:Kombinieren Sie zwei Listen von Datenrahmen, Datenrahmen nach Datenrahmen

Beispieldaten:

names <- c("Cycling1.opr", "Cycling2.opr", "Cycling3.opr") 
mydf1 <- data.frame(V1=c(1:5), V2=c(21:25)) 
mydf2 <- data.frame(V1=c(1:10), V2=c(21:30)) 
mydf3 <- data.frame(V1=c(1:30), V2=c(21:50)) 
opr <- list(mydf1,mydf2,mydf3) 
mydf4 <- data.frame(timestamp=c(1:5)) 
mydf5 <- data.frame(timestamp=c(1:10)) 
mydf6 <- data.frame(timestamp=c(1:30)) 
timestamp <- list(mydf4,mydf5,mydf6) 
names(opr) <- names 
names(timestamp) <- names 

Jede Liste (OPR und Zeitstempel) hat immer die gleiche Anzahl von data.frames und wenn mit Derselbe Name, jeder dieser data.frames hat immer die gleiche Länge. Was ich tun möchte, ist, jeden ähnlich benannten Datenrahmen in einen einzigen Datenrahmen als Teil einer endgültigen Liste zusammenzufassen (vielleicht als finalopr bezeichnet), so dass seine Struktur wie folgt ist.

dput(finalopr) 
list(structure(list(V1 = 1:5, V2 = 21:25, timestamp = 1:5), .Names = c("V1", 
"V2", "timestamp"), row.names = c(NA, -5L), class = "data.frame"), 
structure(list(V1 = 1:10, V2 = 21:30, timestamp = 1:10), .Names = c("V1", 
"V2", "timestamp"), row.names = c(NA, -10L), class = "data.frame"), 
structure(list(V1 = 1:30, V2 = 21:50, timestamp = 1:30), .Names = c("V1", 
"V2", "timestamp"), row.names = c(NA, -30L), class = "data.frame")) 

Antwort

10
> mapply(cbind, opr, timestamp, SIMPLIFY=FALSE) 
$Cycling1.opr 
    V1 V2 timestamp 
1 1 21   1 
2 2 22   2 
3 3 23   3 
4 4 24   4 
5 5 25   5 

$Cycling2.opr 
    V1 V2 timestamp 
1 1 21   1 
2 2 22   2 
3 3 23   3 
4 4 24   4 
5 5 25   5 
6 6 26   6 
7 7 27   7 
8 8 28   8 
9 9 29   9 
10 10 30  10 

$Cycling3.opr 
    V1 V2 timestamp 
1 1 21   1 
2 2 22   2 
3 3 23   3 
4 4 24   4 
5 5 25   5 
6 6 26   6 
7 7 27   7 
8 8 28   8 
9 9 29   9 
10 10 30  10 
11 11 31  11 
12 12 32  12 
13 13 33  13 
14 14 34  14 
15 15 35  15 
16 16 36  16 
17 17 37  17 
18 18 38  18 
19 19 39  19 
20 20 40  20 
21 21 41  21 
22 22 42  22 
23 23 43  23 
24 24 44  24 
25 25 45  25 
26 26 46  26 
27 27 47  27 
28 28 48  28 
29 29 49  29 
30 30 50  30 

Hier ist die Struktur:

> str(mapply(cbind, opr, timestamp, SIMPLIFY=FALSE)) 
List of 3 
$ Cycling1.opr:'data.frame': 5 obs. of 3 variables: 
    ..$ V1  : int [1:5] 1 2 3 4 5 
    ..$ V2  : int [1:5] 21 22 23 24 25 
    ..$ timestamp: int [1:5] 1 2 3 4 5 
$ Cycling2.opr:'data.frame': 10 obs. of 3 variables: 
    ..$ V1  : int [1:10] 1 2 3 4 5 6 7 8 9 10 
    ..$ V2  : int [1:10] 21 22 23 24 25 26 27 28 29 30 
    ..$ timestamp: int [1:10] 1 2 3 4 5 6 7 8 9 10 
$ Cycling3.opr:'data.frame': 30 obs. of 3 variables: 
    ..$ V1  : int [1:30] 1 2 3 4 5 6 7 8 9 10 ... 
    ..$ V2  : int [1:30] 21 22 23 24 25 26 27 28 29 30 ... 
    ..$ timestamp: int [1:30] 1 2 3 4 5 6 7 8 9 10 ... 
+0

Excellent! Einfach wie ich es erwartet hatte. Danke :) – user1912925

+0

kleine Frage: Was ist, wenn dfs in opr und timestamp verschiedene nrows haben? –

+1

@ N.Varela Klingt, als hättest du eine neue Frage. Erwäge das Posten einer neuen, separaten Frage. Sie können in Ihrem Post immer auf diesen Link verweisen. – Thomas

Verwandte Themen