将多个.csv文件合并为一个

Dan*_*nka 3 csv r rbind do.call read.csv

我知道这个问题已经被问过多次了,但是尽管尝试应用上述解决方案,但我还是无法解决我的小问题:

我已经保存了所有要合并到一个文件夹中的.csv文件:

> 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"  
Run Code Online (Sandbox Code Playgroud)

我使用do.calltio将它们全部合并。请注意,所有文件都具有相同的格式。

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

但是,在检查完生成的文件后,我意识到只有第一个文件已导入。是什么导致此问题?

> 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 ...
Run Code Online (Sandbox Code Playgroud)

Mat*_*ett 6

# 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)
Run Code Online (Sandbox Code Playgroud)


raf*_*ira 5

这是一种使用以下方法(可能是最快的方法)将多个.csv文件读取并绑定到一个数据帧中的简单方法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))
Run Code Online (Sandbox Code Playgroud)

而且,如果您要将所有数据文件绑定到数据帧列表中,则只需

# Load data sets
  list.DFs <- lapply(filenames,fread)
Run Code Online (Sandbox Code Playgroud)