[R-SIG-Finance] Creating an index based on a time variable

Brian G. Peterson brian at braverock.com
Wed Oct 21 21:16:02 CEST 2015


You didn't follow the posting guide.  Your example is not reproducible,
and posting HTML that just gets converted to plain text doesn't help.

I assume that you currently have a data.frame as is output by read.csv.

Why are you converting it to a matrix?

I guess that what you want from your data.frame is most likely something
like this:

sector_returns <- read.csv("Sector.csv", head=TRUE)
sector_returns <- xts(sector_returns[,-1],
order.by=as.Date(sector_returns[,1], format="%m/%d/%Y"))


see 

?xts

and

?strftime

for more information.


Regards,

Brian


On Wed, 2015-10-21 at 15:05 -0400, Am Gut wrote:
> Everyone,
> 
> I would appreciate some help on a very basic topic that I have been
> struggling with for quite a while. I am using the xts packages and have
> gone through quite a bit of documentation in search of how to create a time
> index based on a time column in my dataset.
> 
> I am following the code used directly in the xts package documentation and
> would greatly appreciate it if someone would help. The beginning of my
> imported dataset looks like:
> 
> row.names Consumer Discretionary Consumer Staples 1/31/1995 0.6468773
> 2.047813 2/28/1995 3.39433 2.866054 3/31/1995 3.0161 2.887118 4/28/1995
> -1.341188 2.826405 5/31/1995 4.459739 4.309547 6/30/1995 2.636647 1.610363
> 7/31/1995 3.298938 -0.002974272
> 
> I am using the following code:
> 
> require(xts)
> sector_returns = read.csv("Sector.csv", head=TRUE)
> class(sector_returns)
> 
> sector_returns = as.matrix(sector_returns)
> sector_xts = as.xts(sector_returns,dateFormat='Date')
> 
> the last line gives me the following error:
> 
> Error in as.xts.matrix(sector_returns, dateFormat = "Date") :
>   order.by must be either 'rownames()' or otherwise specified
> 
> I have tried a few different ways of using the rownames function and have
> had zero success. I apologize if this question is extremely basic, as I
> have made significant effort trying to get this to work. Any help on
> assigning the rownames to my date variable (row.names column) would be
> extremely helpful.
> 
> Thanks in advance,
> 
> AG
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list