[R] Lag based on Date objects with non-consecutive values
Sam Albers
tonightsthenight at gmail.com
Tue Mar 20 20:18:03 CET 2012
On Mon, Mar 19, 2012 at 9:11 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
>
>
> On Mon, Mar 19, 2012 at 8:03 PM, Sam Albers <tonightsthenight at gmail.com>
> wrote:
>>
>> Hello R-ers,
>>
>> I just wanted to update this post. I've made some progress on this but
>> am still not quite where I need to be. I feel like I am close so I
>> just wanted to share my work so far.
>>
>
> Try this:
>
> Lines <- "Date Dis1
> 1967-06-05 1.146405
> 1967-06-06 9.732887
> 1967-06-07 -9.279462
> 1967-06-08 7.856646
> 1967-06-09 5.494370
> 1967-06-15 5.070176
> 1967-06-16 3.847314
> 1967-06-17 -5.243094
> 1967-06-18 9.396560
> 1967-06-19 4.112792"
>
> # read in data
> library(zoo)
> z <- read.zoo(text = Lines, header = TRUE)
>
> # process it
> g <- seq(start(z), end(z), "day") # all days
> zg <- merge(z, zoo(, g)) # fill in missing days
> lag(zg, 0:-2)[time(z)]
>
Thanks Gabor. I was, however, hoping for base R solution. I think I've
got it and I will post the result here just to be complete. A big
thanks to Brain Cade for an off-list suggestion.
set.seed(32)
df1<-data.frame(
Date=seq(as.Date("1967-06-05","%Y-%m-%d"),by="day", length=5),
Dis1=rnorm(5, 1,10)
)
df2<-data.frame(
Date=seq(as.Date("1967-06-15","%Y-%m-%d"),by="day", length=5),
Dis1=rnorm(5, 1,10)
)
df <- rbind(df1,df2)
df$Dis2 <- df$Dis1*2
lag.base <- function (lag.date, lag.by, lag.var) {
time_dif <- as.numeric(lag.date)-c(rep(NA,lag.by), head(lag.date, -lag.by))
lag.tmp <-c(rep(NA,lag.by), head(lag.var, -lag.by))
lv <- ifelse(time_dif<=lag.by,lag.tmp,NA)
return(lv)
}
df$lag <- lag.base(lag.date=df$Date, lag.var=df$Dis1, lag.by=3);df
df$lag2 <- lag.base(lag.date=df$Date, lag.var=df$Dis2, lag.by=3);df
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
More information about the R-help
mailing list