0

I have a bunch of large CSVs and they all contain the exact same columns and I need to combine them all into a single CSV, so basically appending all the data from each data frame underneath the next. Like this

Table 1

Prop_ID State Pasture Soy Corn
1 WI 20 45 75
2 MI 10 80 122

Table 2

Prop_ID State Pasture Soy Corn
3 MN 152 0 15
4 IL 0 10 99

Output table

Prop_ID State Pasture Soy Corn
1 WI 20 45 75
2 MI 10 80 122
3 MN 152 0 15
4 IL 0 10 99

I have more than 2 tables to do this with, any help would be appreciated. Thanks

SpookyDLX
  • 79
  • 5

2 Answers2

1

A possible solution, in base R:

rbind(df1, df2)

#>   Prop_ID State Pasture Soy Corn
#> 1       1    WI      20  45   75
#> 2       2    MI      10  80  122
#> 3       3    MN     152   0   15
#> 4       4    IL       0  10   99

Or using dplyr:

dplyr::bind_rows(df1, df2)
PaulS
  • 21,159
  • 2
  • 9
  • 26
1

Assuming all the csv files are in a single directory, and that these are the only files in that directory, this solution, using data.table, should work.

library(data.table)
setwd('<directory with your csv files>')
files  <- list.files(pattern = '.+\\.csv$')
result <- rbindlist(lapply(files, fread))

list.files(...) returns a vector containing the file names in a given directory, based on a pattern. Here we ask for only files containing .csv at the end.

fread(...) is a very fast file reader for data.table. We apply this function to each file name ( lapply(files, fread) ) to generate a list containing the contents of each csv. Then we use rbindlist(...) to combine them row-wise.

jlhoward
  • 58,004
  • 7
  • 97
  • 140