我想使用rbind绑定多个数据帧

  • 本文关键字:数据帧 绑定 rbind r
  • 更新时间 :
  • 英文 :


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

fast.rbind()不存在

您可以尝试以下操作-

#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为基数

最新更新