Chaturvedi Dewashish
Chaturvedi Dewashish

Reputation: 1469

Convert data from CSV file time series

I have data like this in my csv file

Date          AAPL      MSFT       GOOG
8/19/2014   100.53  45.78787879     522.7956989
8/18/2014   99.16   45.56565657     517.0967742
8/15/2014   97.98   45.24242424     511.7204301
8/14/2014   97.5    44.71717172     508.1362007
8/13/2014   97.24   44.52525253     506.5232975
8/12/2014   95.97   43.95959596     499.9641577
8/11/2014   95.99   43.63636364     498.8888889
8/8/2014    94.74   43.63636364     494.4086022
8/7/2014    94.48   43.66666667     493.5842294
8/6/2014    94.96   43.17171717     493.5483871

I am reading it like this

price_data <- read.csv("C:\\Prices.csv")

I want to convert it time series. I have seen question at R - Transform Data frame to Time Series and Convert data frame with date column to timeseries.
But, in my case I have more than one column to be converted. No of columns can be varying.

One solution seems to be separate each column and convert to time series and then merge back using cbind.

What is best way to do it.

EDIT
I want to calculate component VaR, using this data. I also have positions of symbol as

MSFT 1000
AAPL 1520
GOOG 398

VaR in package "PerformanceAnalytics" accepts time series. Is there any other way I can pass this data to function?

Upvotes: 2

Views: 3145

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269371

Try this:

Lines <- "Date          AAPL      MSFT       GOOG
8/19/2014   100.53  45.78787879     522.7956989
8/18/2014   99.16   45.56565657     517.0967742
8/15/2014   97.98   45.24242424     511.7204301
8/14/2014   97.5    44.71717172     508.1362007
8/13/2014   97.24   44.52525253     506.5232975
8/12/2014   95.97   43.95959596     499.9641577
8/11/2014   95.99   43.63636364     498.8888889
8/8/2014    94.74   43.63636364     494.4086022
8/7/2014    94.48   43.66666667     493.5842294
8/6/2014    94.96   43.17171717     493.5483871"

library(PerformanceAnalytics)
library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")

rets <- diff(z, arithmetic = FALSE) - 1
VaR(rets)

giving:

            AAPL         MSFT         GOOG
VaR -0.005010481 -0.001461903 -0.001090975

Upvotes: 1

Related Questions