Append\Union两个或几个表合二为一
Append\Union two or several tables into one
我使用 read.table
在 R 中输入了 5 个文本数据集。
每个数据集都具有相同的结构(100 行,50 列)。
我想 union\append 将所有五个 table 合并为一个 table,这将是 500 行 * 50 列。
有人知道怎么做吗?
来自包 dplyr
:
install.packages('dplyr')
library(dplyr)
new_df <- bind_rows(table1, table2, table3, table4, table5)
在基础 R 中,您可以执行以下操作:
# Create some toy data first
nc <- 50
nr <- 1000
# Create five tables with nc columns and nr rows.
df1 <- as.data.frame(replicate(nc, rnorm(nr)))
df2 <- as.data.frame(replicate(nc, rnorm(nr)))
df3 <- as.data.frame(replicate(nc, rnorm(nr)))
df4 <- as.data.frame(replicate(nc, rnorm(nr)))
df5 <- as.data.frame(replicate(nc, rnorm(nr)))
# Join the tables
df <- rbind(df1, df2, df3, df4, df5)
dim(df)
#[1] 5000 50
这会为您提供 5 张堆叠在一起的桌子,如果您正在寻找的话。如果不,
您应该提供最少的示例来说明您的问题。
尽管对于这个特定问题并不重要,但比较不同的 rbind 方法可能仍然有帮助。这是base
、data.table
和dplyr
;
中三种rbind
方法的比较
> dim(df)
[1] 16777216 2
> microbenchmark(rbind(df,df), rbindlist(list(df,df)), bind_rows(df,df), times = 10)
Unit: milliseconds
expr min lq mean median uq max neval cld
rbind(df, df) 3824.4208 4052.6405 4288.5569 4239.2416 4557.5736 4685.2155 10 c
rbindlist(list(df, df)) 272.5048 304.8365 348.0393 357.4388 390.7684 405.0778 10 a
bind_rows(df, df) 571.1732 596.2556 715.1572 643.8038 863.5805 927.0341 10 b
我使用 read.table
在 R 中输入了 5 个文本数据集。
每个数据集都具有相同的结构(100 行,50 列)。
我想 union\append 将所有五个 table 合并为一个 table,这将是 500 行 * 50 列。
有人知道怎么做吗?
来自包 dplyr
:
install.packages('dplyr')
library(dplyr)
new_df <- bind_rows(table1, table2, table3, table4, table5)
在基础 R 中,您可以执行以下操作:
# Create some toy data first
nc <- 50
nr <- 1000
# Create five tables with nc columns and nr rows.
df1 <- as.data.frame(replicate(nc, rnorm(nr)))
df2 <- as.data.frame(replicate(nc, rnorm(nr)))
df3 <- as.data.frame(replicate(nc, rnorm(nr)))
df4 <- as.data.frame(replicate(nc, rnorm(nr)))
df5 <- as.data.frame(replicate(nc, rnorm(nr)))
# Join the tables
df <- rbind(df1, df2, df3, df4, df5)
dim(df)
#[1] 5000 50
这会为您提供 5 张堆叠在一起的桌子,如果您正在寻找的话。如果不, 您应该提供最少的示例来说明您的问题。
尽管对于这个特定问题并不重要,但比较不同的 rbind 方法可能仍然有帮助。这是base
、data.table
和dplyr
;
rbind
方法的比较
> dim(df)
[1] 16777216 2
> microbenchmark(rbind(df,df), rbindlist(list(df,df)), bind_rows(df,df), times = 10)
Unit: milliseconds
expr min lq mean median uq max neval cld
rbind(df, df) 3824.4208 4052.6405 4288.5569 4239.2416 4557.5736 4685.2155 10 c
rbindlist(list(df, df)) 272.5048 304.8365 348.0393 357.4388 390.7684 405.0778 10 a
bind_rows(df, df) 571.1732 596.2556 715.1572 643.8038 863.5805 927.0341 10 b