如何在 R 中垂直连接或合并多个数据集
How do I vertically join or merge multiple datasets within R
我有 12 个数据集都与此相似(这是一个示例,真实数据集都包含超过 10,000 个不同的行,具有相同的 number/name 列)
df1
Start End Duration
9/10/2019 1:00:00 PM 9/10/2019 1:00:10 PM 10
10/10/2019 2:00:00 PM 10/10/2019 2:00:10 PM 10
df2
Start End Duration
11/10/2019 1:00:00 AM 11/10/2019 1:00:10 AM 10
12/10/2019 2:00:00 AM 12/10/2019 2:00:10 AM 10
df3
Start End Duration
01/10/2020 1:00:00 AM 01/10/2020 1:00:10 AM 10
02/10/2020 2:00:00 AM 02/10/2020 2:00:10 AM 10
我想要这样的结果:
Start End Duration
9/10/2019 1:00:00 PM 9/10/2019 1:00:10 PM 10
10/10/2019 2:00:00 PM 10/10/2019 2:00:10 PM 10
11/10/2019 1:00:00 AM 11/10/2019 1:00:10 AM 10
12/10/2019 2:00:00 AM 12/10/2019 2:00:10 AM 10
01/10/2020 1:00:00 AM 01/10/2020 1:00:10 AM 10
02/10/2019 2:00:00 AM 02/10/2019 2:00:10 AM 10
这是我的输出:
structure(list(Start = structure(1:2, .Label = c("11/10/2019 13:00",
"12/10/2019 14:00"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 13:00",
"12/10/2019 14:00"), class = "factor"), Duration = c(10L, 10L
)), class = "data.frame", row.names = c(NA, -2L))
structure(list(Start = structure(1:2, .Label = c("11/10/2019 1:00:00 AM",
"12/10/2019 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 1:00:10 AM",
"12/10/2019 2:00:10 AM"), class = "factor"), Duration = c(10L,
10L)), class = "data.frame", row.names = c(NA, -2L))
structure(list(Start = structure(1:2, .Label = c("1/10/2020 1:00:00 AM",
"2/10/2020 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label = c("1/10/2020 1:00:10 AM",
"2/10/2020 2:00:10 AM"), class = "factor"), Duration = c(10L,
10L)), class = "data.frame", row.names = c(NA, -2L))
这是我试过的:
combined <- rbind(df1, df2)
但是,它仅在连接 2 个数据集而不是 10 个数据集时有效
您可以使用 bind_rows 来自 tidyverse
library(tidyverse)
dim(mtcars) # [1] 32 11
BindMtcars <- bind_rows(mtcars, mtcars, mtcars, mtcars)
dim(BindMtcars) # [1] 128 11
我有 12 个数据集都与此相似(这是一个示例,真实数据集都包含超过 10,000 个不同的行,具有相同的 number/name 列)
df1
Start End Duration
9/10/2019 1:00:00 PM 9/10/2019 1:00:10 PM 10
10/10/2019 2:00:00 PM 10/10/2019 2:00:10 PM 10
df2
Start End Duration
11/10/2019 1:00:00 AM 11/10/2019 1:00:10 AM 10
12/10/2019 2:00:00 AM 12/10/2019 2:00:10 AM 10
df3
Start End Duration
01/10/2020 1:00:00 AM 01/10/2020 1:00:10 AM 10
02/10/2020 2:00:00 AM 02/10/2020 2:00:10 AM 10
我想要这样的结果:
Start End Duration
9/10/2019 1:00:00 PM 9/10/2019 1:00:10 PM 10
10/10/2019 2:00:00 PM 10/10/2019 2:00:10 PM 10
11/10/2019 1:00:00 AM 11/10/2019 1:00:10 AM 10
12/10/2019 2:00:00 AM 12/10/2019 2:00:10 AM 10
01/10/2020 1:00:00 AM 01/10/2020 1:00:10 AM 10
02/10/2019 2:00:00 AM 02/10/2019 2:00:10 AM 10
这是我的输出:
structure(list(Start = structure(1:2, .Label = c("11/10/2019 13:00",
"12/10/2019 14:00"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 13:00",
"12/10/2019 14:00"), class = "factor"), Duration = c(10L, 10L
)), class = "data.frame", row.names = c(NA, -2L))
structure(list(Start = structure(1:2, .Label = c("11/10/2019 1:00:00 AM",
"12/10/2019 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 1:00:10 AM",
"12/10/2019 2:00:10 AM"), class = "factor"), Duration = c(10L,
10L)), class = "data.frame", row.names = c(NA, -2L))
structure(list(Start = structure(1:2, .Label = c("1/10/2020 1:00:00 AM",
"2/10/2020 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label = c("1/10/2020 1:00:10 AM",
"2/10/2020 2:00:10 AM"), class = "factor"), Duration = c(10L,
10L)), class = "data.frame", row.names = c(NA, -2L))
这是我试过的:
combined <- rbind(df1, df2)
但是,它仅在连接 2 个数据集而不是 10 个数据集时有效
您可以使用 bind_rows 来自 tidyverse
library(tidyverse)
dim(mtcars) # [1] 32 11
BindMtcars <- bind_rows(mtcars, mtcars, mtcars, mtcars)
dim(BindMtcars) # [1] 128 11