ARCHIVED: In R, how do I append two data files?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

Assume you have two data files with the same four variable names (id, gender, race, and mathscore), and that datafile1 has 40 cases and datafile2 has 60 cases.

To use the rbind function to append these two data files:

  datafile1 <- read.csv("c:/datafile1.csv", header=T, sep=",")
  datafile2 <- read.csv("c:/datafile2.csv", header=T, sep=",")

  dim(datafile1)
  dim(datafile2)

  datafile <- rbind(datafile1,datafile2)
  dim(datafile)
  
  write.csv(datafile,"c:/datafile.csv")

Above, the read.csv command imports each data file, which is saved in .csv (comma-delimited) format. The dim command shows the dimension of the data in each file; for example, dim(datafile1) returns 40 x 4 (40 cases and 4 variables), dim(datafile2) returns 60 x 4 (60 cases and 4 variables), and dim(datafile) returns 100 x 4 (100 cases and 4 variables). Finally, the write.csv command saves the combined data to the file c:/datafile.csv.

If you want to merge two data files with different variables, see ARCHIVED: How do I merge two data files in R?

If you have questions about using statistical and mathematical software at Indiana University, contact the UITS Research Applications and Deep Learning team.

This is document bcrr in the Knowledge Base.
Last modified on 2018-01-18 17:23:20.

Contact us

For help or to comment, email the UITS Support Center.