我有16个元素的大型列表city
。每个元素代表不同的城市,但尽管结构相同。
head(city[[1]])
Time CET Date Region Temp. Windchill Dew.Point Humidity Pressure
[1,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
[2,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
[3,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
[4,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
[5,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
[6,] Numeric,24 Character,24 Numeric,24 Character,24 Logical,24 Logical,24 Logical,24 Logical,24 Logical,24
Visibility Wind.Dir Wind.Speed
[1,] Logical,24 Logical,24 Logical,24
[2,] Logical,24 Logical,24 Logical,24
[3,] Logical,24 Logical,24 Logical,24
[4,] Logical,24 Logical,24 Logical,24
[5,] Logical,24 Logical,24 Logical,24
[6,] Logical,24 Logical,24 Logical,24
dput(city[[1]][1:6,])
:
structure(list(c(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11), c(12, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), c(12, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11), c(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11), c(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), c(12, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
c("AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM",
"AM", "AM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM",
"PM", "PM", "PM", "PM"), c("AM", "AM", "AM", "AM", "AM",
"AM", "AM", "AM", "AM", "AM", "AM", "AM", "PM", "PM", "PM",
"PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM"), c("AM",
"AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM",
"AM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM",
"PM", "PM", "PM"), c("AM", "AM", "AM", "AM", "AM", "AM",
"AM", "AM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", "PM",
"PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM"), c("AM",
"AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM",
"AM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM",
"PM", "PM", "PM"), c("AM", "AM", "AM", "AM", "AM", "AM",
"AM", "AM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", "PM",
"PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM"), structure(c(15705,
15705, 15705, 15705, 15705, 15705, 15705, 15705, 15705, 15705,
15705, 15705, 15705, 15705, 15705, 15705, 15705, 15705, 15705,
15705, 15705, 15705, 15705, 15705), class = "Date"), structure(c(15706,
15706, 15706, 15706, 15706, 15706, 15706, 15706, 15706, 15706,
15706, 15706, 15706, 15706, 15706, 15706, 15706, 15706, 15706,
15706, 15706, 15706, 15706, 15706), class = "Date"), structure(c(15707,
15707, 15707, 15707, 15707, 15707, 15707, 15707, 15707, 15707,
15707, 15707, 15707, 15707, 15707, 15707, 15707, 15707, 15707,
15707, 15707, 15707, 15707, 15707), class = "Date"), structure(c(15708,
15708, 15708, 15708, 15708, 15708, 15708, 15708, 15708, 15708,
15708, 15708, 15708, 15708, 15708, 15708, 15708, 15708, 15708,
15708, 15708, 15708, 15708, 15708), class = "Date"), structure(c(15709,
15709, 15709, 15709, 15709, 15709, 15709, 15709, 15709, 15709,
15709, 15709, 15709, 15709, 15709, 15709, 15709, 15709, 15709,
15709, 15709, 15709, 15709, 15709), class = "Date"), structure(c(15710,
15710, 15710, 15710, 15710, 15710, 15710, 15710, 15710, 15710,
15710, 15710, 15710, 15710, 15710, 15710, 15710, 15710, 15710,
15710, 15710, 15710, 15710, 15710), class = "Date"), c("EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR"),
c("EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR"), c("EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR"), c("EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR"), c("EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR"), c("EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR", "EPWR",
"EPWR", "EPWR", "EPWR", "EPWR", "EPWR"), c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Dim = c(6L,
12L), .Dimnames = list(NULL, c("Time", "CET", "Date", "Region",
"Temp.", "Windchill", "Dew.Point", "Humidity", "Pressure", "Visibility",
"Wind.Dir", "Wind.Speed")))
最终应该如何看待:
Time CET Date Region Temp. Windchill Dew.Point Humidity Pressure Visibility Wind.Dir Wind.Speed
1 12 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
2 1 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
3 2 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
4 3 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
5 4 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
6 5 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
我想将此列表转换为一行绑定数据帧而不使用循环。 我提出了这种方法,但它涉及循环。
data_whole <- list()
data <- data.frame()
for(i in 1:length(city)){
for(j in 1:nrow(city[[i]])){
data<- bind_rows(data, data.frame(city[[i]][j,]))
}
data_whole[[i]] <- data
}
感谢您的帮助。
答案 0 :(得分:1)
假设显示的dput
数据只是一个list
元素,我们创建了一个包含2个此类元素的list
并使用了rbindlist
library(data.table)
rbindlist(lapply(city, function(x) data.frame(lapply(as.data.frame(x), unlist))))
或tidyverse
获得预期的输出
library(tidyverse)
city %>%
map(as.tibble) %>%
bind_rows %>%
unnest
# A tibble: 288 × 12
# Time CET Date Region Temp. Windchill Dew.Point Humidity Pressure Visibility Wind.Dir Wind.Speed
# <dbl> <chr> <date> <chr> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl> <lgl>
#1 12 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#2 1 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#3 2 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#4 3 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#5 4 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#6 5 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#7 6 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#8 7 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#9 8 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
#10 9 AM 2012-12-31 EPWR NA NA NA NA NA NA NA NA
# ... with 278 more rows
city <- list(city1, city1)