read multiple csv files at once

Hi ,

I have 181 csv files. I want to read them at once so I use the following code to import all of the files at once

return= list.files(pattern="*.csv")
for (i in 1:length(return)) assign(return[i], read.csv(return[i],header = F))

the class of the files are data.frame, I want to convert every data frame to xts object, because my next step is to plug in every xts object to the following function

  # Create an optimized portfolio of returns
  init.portfolio <- portfolio.spec(assets = colnames(log.return))
  init.portfolio <- add.constraint(portfolio = init.portfolio, 
                                   type = "full_investment")
  init.portfolio <- add.constraint(portfolio = init.portfolio, 
                                   type = "long_only")
  init.portfolio <-add.constraint(portfolio = init.portfolio, type="box", min=0, max=1)
  minSD.portfolio <- add.objective(portfolio=init.portfolio, 
  x1<- optimize.portfolio(R = x, portfolio = minSD.portfolio, 
                          optimize_method = "ROI", trace = TRUE)

I want to plug in each one of the xts files here

Minsdr_insample_function(every xts object)

any advice is highly appreciated


Leaving aside the xts issue, here the recommended way to read in multple csv files.

xts-faq.pdf (

I have multiple .csv time-series files that I need to load in a
single xts object. What is the most effcient way to import
the files?
If the files have the same format, load them with read.zoo and then call rbind to join the
series together; finally, call as.xts on the result. Using a combination of lapply and
can accomplish this with very little code:

 filenames <- c("a.csv", "b.csv", "c.csv")
 sample.xts <- as.xts("rbind", lapply(filenames, read.zoo)))

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.