我需要将列表的数据框合并为一个数据框,其中每个数据框之间都有某种分隔或区分。
我的列表:
print(list1)
[[1]]
DATE HHr Tavg RAIN BU FU BUA FUA ABU AFU APP
2 2019-11-19 0 8.655000 10.9935 0 0 0 0 0 0 0
3 2019-11-20 0 9.500000 1.8565 0 0 0 0 0 0 0
4 2019-11-21 0 9.120000 9.3120 0 0 0 0 0 0 0
5 2019-11-22 0 8.150000 9.9430 0 0 0 0 0 0 0
6 2019-11-23 0 8.825000 9.0780 0 0 0 0 0 0 0
7 2019-11-25 4 7.284648 12.7500 0 0 0 0 0 0 0
8 2019-11-26 1 7.284648 12.7500 0 0 0 0 0 0 0
9 2019-11-27 0 7.284648 12.7500 0 0 0 0 0 0 0
10 2019-11-28 0 7.284648 12.7500 0 0 0 0 0 0 0
11 2019-11-29 0 7.284648 12.7500 0 0 0 0 0 0 0
[[2]]
DATE HHr Tavg RAIN BU FU BUA FUA ABU AFU APP
2 2019-11-19 6 24.80000 6.103500 0 0 0 0 0 0 0
3 2019-11-20 8 25.44500 10.302000 1 0 1 0 0 0 0
4 2019-11-21 5 25.57000 0.000000 0 0 1 0 0 0 0
5 2019-11-22 8 26.24000 9.478000 1 0 2 0 0 0 0
6 2019-11-23 11 24.28500 29.974001 2 0 4 0 0 0 0
7 2019-11-25 10 23.48423 2.416667 2 0 6 0 0 0 0
8 2019-11-26 9 23.48423 2.416667 1 0 7 0 0 0 0
9 2019-11-27 16 23.48423 2.416667 4 0 11 0 0 0 0
10 2019-11-28 9 23.48423 2.416667 1 0 12 0 0 0 0
11 2019-11-29 0 23.48423 2.416667 0 0 12 0 0 0 0
我尝试了以下代码,但是它返回了一个完整的运行数据帧,而没有引用原始列表中的数据帧。
data <- ldply(list1)
print(data)
DATE HHr Tavg RAIN BU FU BUA FUA ABU AFU APP
1 2019-11-19 0 8.655000 10.993500 0 0 0 0 0 0 0
2 2019-11-20 0 9.500000 1.856500 0 0 0 0 0 0 0
3 2019-11-21 0 9.120000 9.312000 0 0 0 0 0 0 0
4 2019-11-22 0 8.150000 9.943000 0 0 0 0 0 0 0
5 2019-11-23 0 8.825000 9.078000 0 0 0 0 0 0 0
6 2019-11-25 4 7.284648 12.750000 0 0 0 0 0 0 0
7 2019-11-26 1 7.284648 12.750000 0 0 0 0 0 0 0
8 2019-11-27 0 7.284648 12.750000 0 0 0 0 0 0 0
9 2019-11-28 0 7.284648 12.750000 0 0 0 0 0 0 0
10 2019-11-29 0 7.284648 12.750000 0 0 0 0 0 0 0
11 2019-11-19 6 24.800000 6.103500 0 0 0 0 0 0 0
12 2019-11-20 8 25.445000 10.302000 1 0 1 0 0 0 0
13 2019-11-21 5 25.570000 0.000000 0 0 1 0 0 0 0
14 2019-11-22 8 26.240000 9.478000 1 0 2 0 0 0 0
15 2019-11-23 11 24.285000 29.974001 2 0 4 0 0 0 0
16 2019-11-25 10 23.484231 2.416667 2 0 6 0 0 0 0
17 2019-11-26 9 23.484231 2.416667 1 0 7 0 0 0 0
18 2019-11-27 16 23.484231 2.416667 4 0 11 0 0 0 0
19 2019-11-28 9 23.484231 2.416667 1 0 12 0 0 0 0
20 2019-11-29 0 23.484231 2.416667 0 0 12 0 0 0 0
预期结果:
DATE HHr Tavg RAIN BU FU BUA FUA ABU AFU APP
DATA 1
1 2019-11-19 0 8.655000 10.993500 0 0 0 0 0 0 0
2 2019-11-20 0 9.500000 1.856500 0 0 0 0 0 0 0
3 2019-11-21 0 9.120000 9.312000 0 0 0 0 0 0 0
4 2019-11-22 0 8.150000 9.943000 0 0 0 0 0 0 0
5 2019-11-23 0 8.825000 9.078000 0 0 0 0 0 0 0
6 2019-11-25 4 7.284648 12.750000 0 0 0 0 0 0 0
7 2019-11-26 1 7.284648 12.750000 0 0 0 0 0 0 0
8 2019-11-27 0 7.284648 12.750000 0 0 0 0 0 0 0
9 2019-11-28 0 7.284648 12.750000 0 0 0 0 0 0 0
10 2019-11-29 0 7.284648 12.750000 0 0 0 0 0 0 0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
DATA 2
11 2019-11-19 6 24.800000 6.103500 0 0 0 0 0 0 0
12 2019-11-20 8 25.445000 10.302000 1 0 1 0 0 0 0
13 2019-11-21 5 25.570000 0.000000 0 0 1 0 0 0 0
14 2019-11-22 8 26.240000 9.478000 1 0 2 0 0 0 0
15 2019-11-23 11 24.285000 29.974001 2 0 4 0 0 0 0
16 2019-11-25 10 23.484231 2.416667 2 0 6 0 0 0 0
17 2019-11-26 9 23.484231 2.416667 1 0 7 0 0 0 0
18 2019-11-27 16 23.484231 2.416667 4 0 11 0 0 0 0
19 2019-11-28 9 23.484231 2.416667 1 0 12 0 0 0 0
20 2019-11-29 0 23.484231 2.416667 0 0 12 0 0 0 0
答案 0 :(得分:3)
我们可以使用rbind_list
中的data.table
library(data.table)
rbindlist(list1, idcol = "id")
或将base R
与Map
一起使用
do.call(rbind, Map(cbind, list1, id= seq_along(list1)))
答案 1 :(得分:1)
在基数R中,您可以创建一个id
列,该列将为列表中的每个数据框添加一个唯一的数字。
do.call(rbind, lapply(seq_along(list1),function(x) transform(list1[[x]], id = x)))
# DATE HHr Tavg RAIN BU FU BUA FUA ABU AFU APP id
#2 2019-11-19 0 8.655000 10.993500 0 0 0 0 0 0 0 1
#3 2019-11-20 0 9.500000 1.856500 0 0 0 0 0 0 0 1
#4 2019-11-21 0 9.120000 9.312000 0 0 0 0 0 0 0 1
#5 2019-11-22 0 8.150000 9.943000 0 0 0 0 0 0 0 1
#6 2019-11-23 0 8.825000 9.078000 0 0 0 0 0 0 0 1
#7 2019-11-25 4 7.284648 12.750000 0 0 0 0 0 0 0 1
#8 2019-11-26 1 7.284648 12.750000 0 0 0 0 0 0 0 1
#9 2019-11-27 0 7.284648 12.750000 0 0 0 0 0 0 0 1
#10 2019-11-28 0 7.284648 12.750000 0 0 0 0 0 0 0 1
#11 2019-11-29 0 7.284648 12.750000 0 0 0 0 0 0 0 1
#21 2019-11-19 6 24.800000 6.103500 0 0 0 0 0 0 0 2
#31 2019-11-20 8 25.445000 10.302000 1 0 1 0 0 0 0 2
#41 2019-11-21 5 25.570000 0.000000 0 0 1 0 0 0 0 2
#51 2019-11-22 8 26.240000 9.478000 1 0 2 0 0 0 0 2
#61 2019-11-23 11 24.285000 29.974001 2 0 4 0 0 0 0 2
#71 2019-11-25 10 23.484230 2.416667 2 0 6 0 0 0 0 2
#81 2019-11-26 9 23.484230 2.416667 1 0 7 0 0 0 0 2
#91 2019-11-27 16 23.484230 2.416667 4 0 11 0 0 0 0 2
#101 2019-11-28 9 23.484230 2.416667 1 0 12 0 0 0 0 2
#111 2019-11-29 0 23.484230 2.416667 0 0 12 0 0 0 0 2
与@IceCreamTouCan的建议类似,您可以将dplyr::bind_rows
与.id
参数一起使用
dplyr::bind_rows(list1, .id = "id")
请注意,如果使用命名列表,则会将名称分配为id
列。
数据
list1 <- list(structure(list(DATE = structure(1:10, .Label = c("2019-11-19",
"2019-11-20", "2019-11-21", "2019-11-22", "2019-11-23", "2019-11-25",
"2019-11-26", "2019-11-27", "2019-11-28", "2019-11-29"), class = "factor"),
HHr = c(0L, 0L, 0L, 0L, 0L, 4L, 1L, 0L, 0L, 0L), Tavg = c(8.655,
9.5, 9.12, 8.15, 8.825, 7.284648, 7.284648, 7.284648, 7.284648,
7.284648), RAIN = c(10.9935, 1.8565, 9.312, 9.943, 9.078,
12.75, 12.75, 12.75, 12.75, 12.75), BU = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), FU = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), BUA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), FUA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), ABU = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), AFU = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), APP = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c("2",
"3", "4", "5", "6", "7", "8", "9", "10", "11")), structure(list(
DATE = structure(1:10, .Label = c("2019-11-19", "2019-11-20",
"2019-11-21", "2019-11-22", "2019-11-23", "2019-11-25", "2019-11-26",
"2019-11-27", "2019-11-28", "2019-11-29"), class = "factor"),
HHr = c(6L, 8L, 5L, 8L, 11L, 10L, 9L, 16L, 9L, 0L), Tavg = c(24.8,
25.445, 25.57, 26.24, 24.285, 23.48423, 23.48423, 23.48423,
23.48423, 23.48423), RAIN = c(6.1035, 10.302, 0, 9.478, 29.974001,
2.416667, 2.416667, 2.416667, 2.416667, 2.416667), BU = c(0L,
1L, 0L, 1L, 2L, 2L, 1L, 4L, 1L, 0L), FU = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), BUA = c(0L, 1L, 1L, 2L, 4L, 6L,
7L, 11L, 12L, 12L), FUA = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), ABU = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
AFU = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), APP = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c("2",
"3", "4", "5", "6", "7", "8", "9", "10", "11")))