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

Jason Hart jasonhart4 at icloud.com
Fri Apr 7 03:36:25 CEST 2017


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 RETURNS LOOP FOR MANY FUNDS
# 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")

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20170407/3d0a062c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: example.csv
Type: application/vnd.ms-excel
Size: 16334 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20170407/3d0a062c/attachment.xlb>


More information about the R-SIG-Finance mailing list