[R] Is there simple code for this simple financial time series task?

From: Edwin Hoyle <edwinhoyle_at_yahoo.com>
Date: Tue, 19 Feb 2008 17:41:11 -0800 (PST)


My code below makes a data frame with columns for date, time, and price. Time on each date runs from 1 to 4.

I'd like to add a new column "ts$closingprice", which would have the closing price for that date. To find the closing price, I'd like to take the price in the row having the greatest time value for each date. Then I'd like to fill that closing price into the $closingprice column for all other rows having the same date.

--This appears to be such an easy task, yet is there a simple way to do it that doesn't require a lot of cleverness?

dates<-c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
times<-c(1,2,3,4)
prices<-c(14,15,16,15,15.5,16,14,13,13,11,12,13,14,15,16,15)
ts<-matrix(nrow=16,ncol=3)
ts[,1]<-dates;ts[,2]<-times;ts[,3]<-prices;ts ts<-as.data.frame(ts);
names(ts)<-c("dates","times","prices");ts

  dates times prices

1      1     1   14.0
2      1     2   15.0
3      1     3   16.0
4      1     4   15.0
5      2     1   15.5
6      2     2   16.0
7      2     3   14.0
8      2     4   13.0
9      3     1   13.0
10     3     2   11.0
11     3     3   12.0
12     3     4   13.0
13     4     1   14.0
14     4     2   15.0
15     4     3   16.0
16     4     4   15.0


      ____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 20 Feb 2008 - 01:43:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 20 Feb 2008 - 02:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive