Merge multiple .csv files into one [duplicate]
Asked Answered
C

2

5

I am aware this question has been asked multiple times, but despite of trying to apply the aforementioned solutions i was not able to solve my little problem:

I have saved all my .csv that i am aiming to merge into one folder:

> file_list <- list.files()
> file_list[]
[1] "SR-einfam.csv"           "SR-garage.csv"           "SR-hotel.csv"           
[4] "SR-IndustrieGewerbe.csv" "SR-mehrfam.csv"          "SR-OffG.csv"  

the I use a do.call tio merge them all. Note that all the files have the same format.

sr.master <- do.call("rbind", lapply(file_list, read.csv,  sep = ";", header = TRUE)) 
names(sr.master)
str(sr.master)

however after inspecting my resulting file I have realized that only the first file has been imported. What causes this problem?

> str(sr.master)
'data.frame':   1941 obs. of  8 variables:
 $ Berechnung: Factor w/ 51 levels "Berechnung 1",..: 51 1 12 23 34 45 47 48 49 50 ...
 $ Situation : Factor w/ 13 levels "Nach Massnahme 0",..: 6 6 6 6 6 6 6 6 6 6 ...
 $ Sachrisiko: num  1857 1857 1857 1337 1342 ...
 $ PID       : int  2844 2844 2844 2844 2844 2844 2844 2844 2844 2844 ...
 $ Case      : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Differenz : num  0 0 0 -28 -27.7 ...
 $ Prozess   : Factor w/ 1 level "Murgang": 1 1 1 1 1 1 1 1 1 1 ...
 $ Objektart : Factor w/ 1 level "Einfamilienhaus": 1 1 1 1 1 1 1 1 1 1 ...
Coordination answered 2/6, 2017 at 12:47 Comment(7)
Without a working example, it is pretty difficult to diagnose. I suggest that you initially split the do.call(.. lapply...) into two separate steps. First, something like myData <- lapply(...). Now take a look at the resulting list. check names of datasets. Make sure it contains what you think it should, perform str on each element of the list, etc.Nevis
Any warnings when you run only lapply(file_list, read.csv, sep = ";", header = TRUE)?Alcina
@Jimbou, sep = "," for csv files not ;Frannie
@Parth Chaudhary my data is separated by ";"Coordination
@Danka, just a thought ... try rbind after reading files individually. Is it working ?Frannie
@Parth Chaudhary and everyone else - thank you for your help. I just realized that the problem is in the indivudual files. The merging actually works with my code as written in the question. - Should i delete this question? I mean it is wrong question and will not help anyone...Coordination
@Danka you should delete this. It may eventually be closed and then deleted, but you can save the community the trouble by doing it yourself.Nevis
D
7
# Get file list
  file_list <- list.files()

# Read all csv files in the folder and create a list of dataframes
  ldf <- lapply(file_list , read.csv)

# Combine each dataframe in the list into a single dataframe
  df.final <- do.call("rbind", ldf)
Dukas answered 2/6, 2017 at 13:54 Comment(1)
This answer worked best for me as it loaded the first row as the top row. ThanksWaterway
W
5

Here is a simple way (and probably the fastest one) to read andbind multiple .csv files into one single data frame using fread{data.table}

# Load library
  library(data.table)

# Get a List of all files in directory named with a key word, say all `.csv` files
  filenames <- list.files("C:/your/folder", pattern="*.csv", full.names=TRUE)

 # read and row bind all data sets
   data <- rbindlist(lapply(filenames,fread))

And in case you want to bind all data files into a list of data frames, it's as simple as

# Load data sets
  list.DFs <- lapply(filenames,fread)
Winburn answered 2/6, 2017 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.