[R-SIG-Finance] Performanceanalytics -- table.calendarreturns question

Enrico Schumann es at enricoschumann.net
Fri Apr 7 22:42:20 CEST 2017

On Fri, 07 Apr 2017, Jason Hart writes:

> Hello - I'm hoping someone has solved this already but I am trying to run the table.calendarreturns function in
> performanceanalytics for many funds and then export the data to a csv file (I'm interested in comparing monthly
> returns between funds).  The code itself runs fine, it's the presentation in the CSV file that's a problem.  I'd
> like to have the output for each fund show up vertically on top of each other in the csv file the exact same way
> the output shows up in the console after the code is run.    Right now the way the data is exported all the
> monthly returns for the funds show up in one long horizontal row for each year.  Evidently I need to add another
> line or two of code to manipulate the data for export
> Thanks for any help!
> Here's an example along w/ a sample data template
> library(PerformanceAnalytics)
> library(timeSeries)
> library(fPortfolio)
> library(quantmod)
> library(caTools)
> returns <- read.csv("example.csv")
> lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
> returns[,1] = as.Date(as.character(returns[,1]),format="%d-%b-%y")
> Sys.setlocale("LC_TIME", lct)
> returns.saved <- returns ## Saves the data if it needs to be reset later.
> start.date <- returns[,1][1] ##Default start date; use this when you want entire data set
> TickerListexcel <- colnames(returns[,-1])
> #Creating time series object
> returns = timeSeries(returns[,-1], charvec = returns[,1])
> #colnames(returns) <- abbreviate(colnames(returns),minlength=6)
> str(returns)
> returns
> # Calendar Tables
> cal.list <- list()
> for (i in 1:dim(returns)[2]) {
> cal.list[[i]] <- table.CalendarReturns(returns[,i], digits=2, as.perc = TRUE)
> }
> names(cal.list) <- TickerListexcel
> write.csv(cal.list,file="Calendar Year Returns.csv")

Do you mean the output of print(returns) or of

In either case, if you want to store the results as
they show up in the console, you could use
?capture.output, as in

   capture.output(cal.list, file = "returns.txt")

Enrico Schumann
Lucerne, Switzerland

More information about the R-SIG-Finance mailing list