简体   繁体   English

R:合并 >2 个数据帧并保留列标题

[英]R: Merge >2 dataframes and keep column headers

I found a great function someone wrote here , to merge >2 dataframes in R.我发现有人在这里写了一个很棒的 function,用于在 R 中合并 >2 个数据帧。 However, when I use it on dataframes with column headers, the headers are erased and replaced with "X1", "X2", etc.但是,当我在带有列标题的数据帧上使用它时,标题会被删除并替换为“X1”、“X2”等。

x<-matrix(1:10,5,2)
y<-matrix(1:16, 4,4)
z<-matrix(1:12, 2,6)

require(plyr) # requires plyr for rbind.fill()
cbind.fill <- function(...) {                                                                                                                                                       
  transpoted <- lapply(list(...),t)                                                                                                                                                 
  transpoted_dataframe <- lapply(transpoted, as.data.frame)                                                                                                                         
  return (data.frame(t(rbind.fill(transpoted_dataframe))))                                                                                                                          
} 

cbind.fill(x,y,z)

Sorry that I don't have an example with actual column names!抱歉,我没有实际列名的示例!

If you like tidyverse solutions, a dplyr bind should work.如果您喜欢 tidyverse 解决方案,dplyr 绑定应该可以工作。 Plenty of examples online with vignettes大量带有小插图的在线示例

https://dplyr.tidyverse.org/reference/bind.html https://dplyr.tidyverse.org/reference/bind.html

Edit: if you post sample data and what you want your output to look like, it would be easier to help you write some code编辑:如果您发布示例数据以及您希望 output 的样子,帮助您编写一些代码会更容易

library(tidyverse)
head(df) %>% dput()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM