通过数据帧组合两个数据帧列表,数据帧 [英] Combine two lists of dataframes, dataframe by dataframe

查看:148
本文介绍了通过数据帧组合两个数据帧列表,数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无疑是一个简单的问题。我已经花了最后一个小时左右的时间来寻找解决方案,但显然缺少一些东西。如果这确实是重复的,请链接我正确的方式来做到这一点:



示例数据:

 名称<  -  c(Cycling1.opr,Cycling2.opr,Cycling3.opr)
mydf1< - data.frame(V1 = c :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 )
时间戳列表(mydf4,mydf5,mydf6)
名称(opr)< - 名称
名称(时间戳)< - 名称

每个列表(opr和timestamp)始终具有相同数量的数据框架,当具有相同的名称时,每个数据框架总是相同的长度。我想做的是将每个类似命名的数据帧合并为单个数据框,作为最终列表(可能命名为finalopr)的一部分,使其结构如下。

  dput(finalopr)
list(structure(list(V1 = 1:5,V2 = 21:25,时间戳= 1:5),.Names = c(V1,
V2,timestamp),row.names = c(NA,-5L),class =data.frame
结构(列表(V1 = 1:10,V2 = 21:30,timestamp = 1:10),.Names = c(V1,
V2,timestamp .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))


解决方案

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

$ Cycling2.opr
V1 V2时间戳
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时间戳
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))
3
$ Cycling1.opr:'data.frame'的列表:5个obs。的3个变量:
.. $ 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。的3个变量:
.. $ 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。的3个变量:
.. $ 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 ...
/ pre>

I have what is no doubt a simple problem. I have spent the last hour or so looking around for a solution but am clearly missing something. If this is indeed a duplicate please link me to the right way to do this:

Example data:

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

Each list (opr and timestamp) always has the same number of data.frames and when having the same name, each of these data.frames is always the same length. What I would like to do is merge each similarly named dataframe into a single dataframe as part of a final list (perhaps named finalopr) such that its structure is as follows.

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"))

解决方案

> 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

Here's the structure:

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

这篇关于通过数据帧组合两个数据帧列表,数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆