0

I am new to R and have a folder containing ~300 .csv files. I am trying to merge all the data into one file so that I can carry out some basic arithmetic on it. I have tried searching the web however the function I have tried keep bringing me errors eg

 master <- list.files(path = "~/")
master <- do.call("rbind", lapply("001.csv" "002.csv", read.csv, header = TRUE))

gives back Error: unexpected string constant in ""329.csv" "330.csv"" etc

and the following

master <- do.call( rbind, lapply(`001.csv`,`002.csv` function(nam){ 
  cbind(name=nam, read.file(paste0(nam,".csv"), header=TRUE) )
}))

gives Error: unexpected '}' in "}"

Would anyone be able to help? If so could you please explain so that someone who is new to programming will be able to follow.

Thank you

Rana
  • 11
  • 1
  • 4
    [This](http://stackoverflow.com/questions/30242065/trying-to-merge-multiple-csv-files-in-r), or maybe [this](http://stackoverflow.com/questions/20666073/how-to-combine-multiple-csv-files-in-r) – Sotos Mar 15 '16 at 14:57
  • The first error means that you cannot use `"001.csv" "002.csv"` . Maybe try `list("001.csv", "002.csv"`) instead. – bet.bom Mar 15 '16 at 15:03
  • 1
    @Sotos if it's a duplicate please flag as such – alexwhitworth Mar 15 '16 at 16:10

2 Answers2

0

Hi I think your code almost worked, you forgot to put the file names in a vector. You could make a sequence of the file names with

do.call("rbind",lapply(c("001.csv", "001.csv"), read.csv, header = T))

If the files are all in the form "001.csv" you could use the following function that automates the names of the csv files, for example if we have 001.csv until 10.csv you could use:

pollutantmean <- function(idrange) {
  output <- c()
  for(id in idrange){
    if((id>=1) && (id<10)) {
      output <- c(output, paste("00",id,sep=""))
    } else if((id>=10) && (id<=99)) {
      output <- c(output, paste("0",id,sep=""))
    } else {
      output <- c(output,id)
    }}
  output
}


do.call("rbind",lapply(paste0(pollutantmean(1:10),".csv"), read.csv, header = T))

Credits to Using colon to specify a sequence in function which I used to create the pollutantmean function.

Community
  • 1
  • 1
Tobias Dekker
  • 980
  • 8
  • 19
0

In the end I used this and it seemed to work.

files_full <- list.files("specdata", full.names=TRUE)

dat <- data.frame()
    for (i in 1:332) {
      dat <- rbind(dat, read.csv(files_full[i]))
    }
    str(dat)

Thanks for all your help

Rana
  • 11
  • 1