首页 > 解决方案 > 我想使用 rbind 绑定多个数据框

问题描述

我想绑定五个数据框 df1、df2、df3、df4 和 df5。但是,使用 fast.rbind() 返回错误说

找不到 fast.rbind()。

标签: r

解决方案


您可以尝试以下方法 -

#get the 5 dataframes in a list
list_df <- mget(paste0('df', 1:5))
#Get the common column names
common_cols <- Reduce(intersect, lapply(list_df, colnames))
#select only the common columns from each dataframe and bind it to one dataframe.
result <- purrr::map_df(list_df, `[`, common_cols)

或者最后一步可以改为

result <- do.call(rbind, lapply(list_df, `[`, common_cols))

将所有内容保留在基础 R 中。


推荐阅读