[R] Add an NA column to a zoo object

jpm miao miaojpm at gmail.com
Thu Oct 23 04:30:29 CEST 2014


Hi ,

  I have a zoo object with several rows fx, r3m, etc. I would like to
create in the zoo object the lag of one column. One thing I can think of is
to create the lag first and then merge it into the original zoo object,
which is tedious. Can I add an NA column to the zoo object first and then
put the lag into the NA column? Thanks!


datz<-zoo(dat, dat[,"date1"])
fxlag3m<-lag(datz[,"fx"], -65, na.pad=TRUE)
datz<-merge(datz, fxlag3m)
> head(datz)
           date       fx     r3m     r6m     r1y     date1      fxlag3m
2009-01-01 2009-01-01  90.90 -7.3500 -7.8500 -8.3000 2009-01-01 <NA>
2009-01-02 2009-01-02  91.16 -7.3458 -8.0487 -8.4645 2009-01-02 <NA>
2009-01-05 2009-01-05  93.19 -6.9000 -7.6000 -8.2500 2009-01-05 <NA>
2009-01-06 2009-01-06  93.97 -6.7000 -7.3500 -8.0500 2009-01-06 <NA>
2009-01-07 2009-01-07  93.23 -6.3500 -7.1000 -7.7000 2009-01-07 <NA>
2009-01-08 2009-01-08  91.63 -6.6500 -7.3500 -7.8000 2009-01-08 <NA>

	[[alternative HTML version deleted]]



More information about the R-help mailing list