合并两个 Dataframe 列表,逐个 Dataframe

7eumitmz  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(144)

示例数据:

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

每个列表(opr和timestamp)总是有相同数量的data.frames,并且当具有相同的名称时,这些data.frames中的每个总是具有相同的长度。我想做的是将每个名称相似的 Dataframe 合并为一个 Dataframe ,作为最终列表(可能命名为finalopr)的一部分,其结构如下所示。

final_opr <- read.table(header = TRUE, text = "
      z.surv.mos raceeth year.2cat pt               nevent ncensor nrisk cum.ev cum.cen pointflg pe               se               lower.cl         upper.cl
'38'  36.        1.      1.        0.10896243930756 9.     0.      311.  2474.  1.      1.       0.89103756069243 0.00591553159512 0.09796374785164 0.12119598770184
'134' 36.        1.      2.        0.12919986395988 3.     9.      96.   2469.  958.    1.       0.87080013604011 0.00860912091676 0.11338170396883 0.14722485430136
'183' 36.        2.      1.        0.10763696166101 0.     0.      33.   287.   4.      1.       0.89236303833898 0.01746946721576 0.07830897003442 0.14794876641234
'246' 36.        2.      2.        0.0918969557367  0.     1.      9.    342.   107.    1.       0.90810304426329 0.01975702415208 0.06029765195198 0.1400560419898
'289' 36.        3.      1.        0.14186152615109 2.     0.      72.   440.   12.     1.       0.8581384738489  0.01550071018085 0.11451353670001 0.17574073058836
'366' 36.        3.      2.        0.12701814940611 1.     2.      21.   496.   198.    1.       0.87298185059388 0.01904081251339 0.09468155080317 0.17039866945242
'412' 36.        4.      1.        0.05405405405405 0.     0.      2.    35.    0.      1.       0.94594594594594 0.03717461110299 0.01404207131432 0.20807761862723
'452' 36.        4.      2.        0.09393141727008 0.     1.      2.    40.    13.     1.       0.90606858272991 0.05797150600236 0.02802051731609 0.31488038035974
'491' 36.        5.      1.        0.08880901672474 1.     0.      48.   505.   19.     1.       0.91119098327525 0.01228353765126 0.06772108402588 0.11646360310182
'563' 36.        5.      2.        0.11716939090588 1.     0.      20.   616.   239.    1.       0.88283060909411 0.01608823714602 0.08952365586359 0.15335238527538
")
c7rzv4ha

c7rzv4ha1#

> 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

结构如下:

> 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 ...

相关问题