[R] Marking Interval Length

Jia Ying Mei jiamei at Princeton.EDU
Sat Jul 26 17:30:56 CEST 2008


Hi everyone,

I'm trying to break down data into intervals because I need to analyze 
data according to the intervals between each change in value. What I 
have done is merged the attached data files, and filled in the missing 
values this way:

 > fmt<-"%m/%d/%y"
 > dd<-read.csv("Desktop/R/CDSdate.txt")
 > dd$Date<-as.Date(dd$Date, fmt)
 >library(zoo)
 > data<-read.zoo("Desktop/R/Countrydata.txt", head=T, format=fmt, sep="\t")
 >newdata<-merge(data, zoo(,dd$Date)
 >Finaldata<-na.locf(newdata, fromLast=TRUE)

Now, I have to break up this Finaldata. I've been told to do something 
like this:

# first make up some data
jym.df<-data.frame(country=rep(c("au","br""cn","in","us"),each=100),
 date=rep(as.Date("02/02/2008",
  format="%d/%m/%Y"),500)+sample(-40:40,500,TRUE),
 price=rnorm(500,100,20))
# get the intervals by country - have to create a new data frame
jymint.df<-data.frame(country=rep(levels(jym.df$country),each=99),
 intervals=unlist(tapply(jym.df$date,jym.df$country,diff)))
library(prettyR)
# break down the intervals by country
brkdn(intervals~country,jymint.df,num.desc=c("mean","median"))

But, obviously the first step generates data that is very different from 
my data. Does anyone know what I can do to modify the above directions 
so that it can be applied to my data?

I've been told by a friend to do something like either:

-loop through countries using na.omit so you get a column of different 
length for each country. then, run the code.

-or, write a code that detects changes in the difference of prices - if 
positive, you have a new interval. use this information to mark 
intervals and take their length.

I am not familiar with either way of dealing with this, and was also 
told this may be complicated, so any help would be greatly appreciated! 
Thanks!

Jia Ying Mei
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CDSdate.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080726/5b2470f5/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Countrydata.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080726/5b2470f5/attachment-0001.txt>


More information about the R-help mailing list