4

Possible Duplicate:
Read multiple CSV files into separate data frames

I need to read many csv files into dataframes from one folder. The csv file names are of the form fxpair-yyyy-mm.csv (e.g. AUDJPY-2009-05.csv). I want to read all csv files in and create dataframes of the form fxpair.yyyy.mm

I am having trouble creating the dataframe names in the loop for assignment from the read.csv statements

filenames <- list.files(path=getwd())  
numfiles <- length(filenames)  

#fx.data.frames to hold names that will be assigned to csv files in csv.read
fx.data.frames <- gsub(pattern="-",x=filenames,replacement=".")  
fx.data.frames <- gsub(pattern=".csv",x=fx.data.frames,replacement="")

i <-1  
for (i in c(1:numfiles)){  
   filenames[i] <- paste(".\\",filenames[i],sep="")  
   fx.data.frames[i] <- read.csv(filenames[i], header=FALSE)
}

The csv.read seems to work fine but I am not able to create the dataframe objects in the way I intend. I just want some way to name the dataframes read in the fxpair.yyyy.mm format based on the file name.

Am I missing something obvius? THANK YOU FOR ANY HELP!!

Community
  • 1
  • 1
user674042
  • 41
  • 1
  • 1
  • 2
  • also see another stackoverflow question involving reading multiple text files into R http://goo.gl/jM1J7. Keep in mind that it is easier to deal with a list of data frames in R, rather than writing each file to a different data frame, as processing becomes easier. – Ramnath Mar 24 '11 at 00:48
  • 2
    On a sidenote : in R you don't have to initialize i, and you can just use `for (i in 1:numfiles)` and drop the `c`. Even better, just use `for (i in filenames)` in your loop, saves you a lot of hassle. – Joris Meys Mar 24 '11 at 00:57

2 Answers2

7

Just to illustrate my comment :

for (i in filenames){  
   name <- gsub("-",".",i)
   name <- gsub(".csv","",name)  
   i <- paste(".\\",i,sep="")
   assign(name,read.csv(i, header=FALSE)
}

Or, to save all dataframes in a list :

All <- lapply(filenames,function(i){
    i <- paste(".\\",i,sep="")
    read.csv(i, header=FALSE)
})
filenames <- gsub("-",".",filenames)
names(All) <- gsub(".csv","",filenames)

I'd go for the second solution, as I like working with lists. It's less of a hassle to clean up the workspace afterwards. You also get rid of the name and i clutter in the global environment. These might cause some funny bugs later in the code if you're not careful. See also Is R's apply family more than syntactic sugar?

Community
  • 1
  • 1
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
5

How about this :

for (i in c(1:numfiles)){  
    filenames[i] <- paste(".\\",filenames[i],sep="")  
    assign(gsub("[.]csv$","",filenames[i]),read.csv(filenames[i], header=FALSE))
}
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
  • apologies, i am relatively new to R, but I don't see how this creates dataframes named fxpair.yyyy.mm. I subbed in this code, ran it and then tried a ls() to make sure all of the csv files came in as dataframes named correctly but nothing other than "filenames" "fx.data.frames" and "i" exist. Thank you. – user674042 Mar 24 '11 at 00:37