0

Is there a way to reprint column headers directly below the last row of the first data set (directly above the second data set) when using rbind to put two data sets together? I have searched and searched but haven't seen any examples like this. Thanks!

Al J
  • 3
  • 2
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Ronak Shah Aug 16 '18 at 04:20
  • What do you mean by "reprint" ? The only context in which this might make sense would be a table in a document. In the console, `rbind` on two data frames generates a new data frame; headers are at the head by definition. – neilfws Aug 16 '18 at 04:28

2 Answers2

0

A not so sophisticated but this will work for you.

Import both the data frames with keeping header = F

after that use

library(dplyr)
final<- bind_rows(df1,df2) ##this will bind both the data frames

names(final) <- final[1,] ##this will take 1st row as column names or header
final <- final[-1,] ##this will remove your 1st row which is not useful now.

This method will help you do your work.

Hunaidkhan
  • 1,411
  • 2
  • 11
  • 21
0

I generated some example data (easier if you provide this yourself when asking the question). Basically, you take the column names of the second dataframe, convert these to a dataframe object. You also need to setNames function to give each dataframe that you want to rbind the same column names as the first dataframe.

df1 <- data.frame(one=c("a", "b"), two=c("c", "d"))
df1

#  one two
#1   a   c
#2   b   d

df2 <- data.frame(three=c("e", "f"), four=c("g", "g"))
df2

#  three four
#1     e    g
#2     f    g

rbind(df1, 
      setNames(as.data.frame(t(colnames(df2))), names(df1)), 
      setNames(df2, names(df1)))

#    one  two
#1     a    c
#2     b    d
#3 three four
#4     e    g
#5     f    g
milan
  • 4,782
  • 2
  • 21
  • 39