Sivanantham C Sivanantham C - 1 year ago 79
R Question

Issue in Loading multiple .csv files into single dataframe in R using rbind

I have written the following function to combine 300 .csv files.My directory name is "specdata". I have done the following steps for execution,

step 1:

> x <- function(directory) {

dir <- directory

data_dir <- paste(getwd(),dir,sep = "/")

files <- list.files(data_dir,pattern = '\\.csv')

tables <- lapply(paste(data_dir,files,sep = "/"), read.csv, header = TRUE)

pollutantmean <- , tables)


step 2:


step 3:


Error in head(pollutantmean) : object 'pollutantmean' not found

What is the mistake I have done into this . Can you please anyone explain ?

Thanks in Advance.

Answer Source

There's a lot of unnecessary code in your function. You can simplify it to:

load_data <- function(path) { 
  files <- dir(path, pattern = '\\.csv', full.names = TRUE)
  tables <- lapply(files, read.csv), tables)

pollutantmean <- load_data("specdata")

Be aware that + rbind is relatively slow. You might find dplyr::rbind_list or data.table::rbindlist to be substantially faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download