0

I found a great function someone wrote here, to merge >2 dataframes in R. However, when I use it on dataframes with column headers, the headers are erased and replaced with "X1", "X2", etc.

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!

j400nb
  • 17
  • 5

1 Answers1

0

If you like tidyverse solutions, a dplyr bind should work. Plenty of examples online with vignettes

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

library(tidyverse)
head(df) %>% dput()
OTA
  • 269
  • 2
  • 17