[R] Reading multiple csv files

Patrick Connolly p_connolly at slingshot.co.nz
Fri Feb 26 09:48:32 CET 2010

On Fri, 26-Feb-2010 at 12:15AM -0800, Madhavi Bhave wrote:

|> Dear R helpers
|> ?

|> Some particular analysis leads me to various number of output csv
|> files depending on some conditions. Say e.g. I have output files
|> variable1.csv, variable2.csv, .............. Problem is I don't
|> know how many csv files been generated. They could be 4, 5 or even
|> 10. Each file will have a column called amount.  ?

|> My problem is to find filewise mean(amount) and sd(amount). I need
|> to write a loop where all these individual csv files will be read
|> and after reading each file, mean and sd will be calculated.  ?

|> I have tried to write some R code which is very absurd. 
|> ?
|> for (i in 1 : n)? # n is no of input files

There's a bit of a problem with your character encoding.  On my
system, I get that question mark which doesn't make syntactic sense.
Just what you have there, I can't guess, so it's a bit hard to work
out what could be the problem (apart from not knowing what size n
should be).

|> ?
|> {
|> data[i] = read.csv(file = paste("variable", i, ".csv", sep = ""))$amount
|> mean(data[i])
|> sd(data[i])

You'll need to tell us what your object 'data' is.  You probably need a
list, in which case data[[i]] might work.

|> }
|> ?
|> I get following error.
|> ?
|> Error in file(file, "rt") : cannot open the connection
|> In addition: Warning message:
|> In file(file, "rt") :
|> ? cannot open file 'paste("output", i, ".csv", sep = "")': Invalid argument
|> ?
|> Please guide

The posting guide might have given you a few ideas.  We don't know
what you did that used "output" in it to have any idea what could be
behind the Invalid argument message.

|> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html


   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt

More information about the R-help mailing list