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

Edwin Hoyle edwinhoyle at yahoo.com
Wed Feb 20 02:41:11 CET 2008


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.



More information about the R-help mailing list