[R] Interpolating hourly basis

R. Michael Weylandt michael.weylandt at gmail.com
Mon Nov 21 20:01:58 CET 2011


You didn't dput() your data as I asked so I can't work with it, but,
to get you started, here's an example of how you could do
interpolation of the sort you seem interested in using xts objects.
(It also seems like you need to decide what to make of the repeated
measurements before trying to extrapolate: do you want to average them
and use those values to interpolate or do something to take advantage
of repeated measurements?)

library(xts)
X <- xts( sin( (0:50)/2), Sys.Date()+7*(0:50))

X.interp <- xts( rep(NA, 351), Sys.Date()+0:350)
X.interp[time(X)] <- X

plot(X)
points(na.locf(X.interp), col = 3)
points(na.spline(X.interp), col = 2)

I don't think you need to change the second column of your data frame
to a date object: that information is encapsulated in the timestamps
you constructed. I'd use the constructor as
dat.xts=xts(dat[,-2],order.by=base_time)

I hope this helps,

Michael

On Sun, Nov 20, 2011 at 7:35 AM, abcdef ghijk <linehunk at yahoo.com> wrote:
> Hi Weylandt,
>
> i am novice to R but i tried function like merge but it dont work with my
> data because i have similar times multiple times. I will try to reclarify my
> quetion and hope it will be more clear:
> I have a data that is being updated every 6 hours for next eight days at a
> span of 3 days.
> i have coded it as below:
> library('xts')
> library('zoo')
> Sys.setenv(TZ='Asia/Calcutta')# setting current time zone to Indian Standard
> Time
> datII=read.table(paste('D:/shankar/R_workspace/MyRWork/data/nwp-II.dat_II.dep',sep=","),head=FALSE,fill
> = TRUE)
> #forecast_time=strptime(as.character(dat[,2]),format='%Y%m%d%H%M',tz="Asia/Calcutta")
> base_time=strptime(as.character(dat[,1],dat[,2]),format='%Y%m%d%H%M',tz="Asia/Calcutta")
> #approxfun(forecast_time)
> dat.xts=xts(dat[,-1],order.by=base_time)
> executing dat.xts comes like as below:
> i tried to change the second column (v2) also in date format but couldn't
> succeed.
> My problem is i want to extrapolate the series on hourly basis as compared
> to three hours.
> Besides V5 and V6 is not impotance. Hope i am clear. Thanks in advance.
>
>                                               V2     V3     V4 V5 V6
> 2010-05-01 00:00:00 201005010000  1.72 291.38  1  0
> 2010-05-01 00:00:00 201005010300  0.95 334.79  1  0
> 2010-05-01 00:00:00 201005010600  2.25  57.00  1  0
> 2010-05-01 00:00:00 201005010900  0.43  14.73  1  0
> 2010-05-01 00:00:00 201005011200  0.75 100.08  1  0
> 2010-05-01 00:00:00 201005011500  2.20 149.00  1  0
> 2010-05-01 00:00:00 201005011800  1.96 158.75  1  0
> 2010-05-01 00:00:00 201005012100  0.94 228.24  1  0
> 2010-05-01 00:00:00 201005020000  0.93 270.77  1  0
> 2010-05-01 00:00:00 201005020300  1.40 271.91  1  0
> 2010-05-01 00:00:00 201005020600  1.55   3.50  1  0
> 2010-05-01 00:00:00 201005020900  2.05  63.42  1  0
> 2010-05-01 00:00:00 201005021200  2.75 100.32  1  0
> 2010-05-01 00:00:00 201005021500  3.83 109.04  1  0
> 2010-05-01 00:00:00 201005021800  1.61 149.33  1  0
> 2010-05-01 00:00:00 201005022100  1.32 167.59  1  0
> 2010-05-01 00:00:00 201005030000  1.74 177.71  1  0
> 2010-05-01 00:00:00 201005030300  1.79 192.50  1  0
> 2010-05-01 00:00:00 201005030600  0.13  97.95  1  0
> 2010-05-01 00:00:00 201005030900  2.15 103.36  1  0
> 2010-05-01 00:00:00 201005031200  2.96 110.35  1  0
> 2010-05-01 00:00:00 201005031500  2.75 163.97  1  0
> 2010-05-01 00:00:00 201005031800  3.16 199.21  1  0
> 2010-05-01 00:00:00 201005032100  2.11 223.85  1  0
> 2010-05-01 00:00:00 201005040000  1.42 255.40  1  0
> 2010-05-01 00:00:00 201005040300  2.37 274.25  1  0
> 2010-05-01 00:00:00 201005040600  1.19  18.93  1  0
> 2010-05-01 00:00:00 201005040900  1.16  83.48  1  0
> 2010-05-01 00:00:00 201005041200  1.65  75.87  1  0
> 2010-05-01 00:00:00 201005041500  0.49  86.23  1  0
> 2010-05-01 00:00:00 201005041800  1.32 183.03  1  0
> 2010-05-01 00:00:00 201005042100  1.04 212.19  1  0
> 2010-05-01 00:00:00 201005050000  1.36 279.77  1  0
> 2010-05-01 00:00:00 201005050300  2.75 283.78  1  0
> 2010-05-01 00:00:00 201005050600  1.20   3.76  1  0
> 2010-05-01 00:00:00 201005050900  1.43  86.78  1  0
> 2010-05-01 00:00:00 201005051200  1.02 108.71  1  0
> 2010-05-01 00:00:00 201005051500  1.10 210.81  1  0
> 2010-05-01 00:00:00 201005051800  1.48 239.43  1  0
> 2010-05-01 00:00:00 201005052100  1.52 257.16  1  0
> 2010-05-01 00:00:00 201005060000  1.26 249.03  1  0
> 2010-05-01 00:00:00 201005060300  1.83 271.65  1  0
> 2010-05-01 00:00:00 201005060600  0.10 111.22  1  0
> 2010-05-01 00:00:00 201005060900  1.33  69.26  1  0
> 2010-05-01 00:00:00 201005061200  1.49 130.34  1  0
> 2010-05-01 00:00:00 201005061500  1.65 185.87  1  0
> 2010-05-01 00:00:00 201005061800  1.99 213.90  1  0
> 2010-05-01 00:00:00 201005062100  1.10 223.54  1  0
> 2010-05-01 00:00:00 201005070000  1.58 198.25  1  0
> 2010-05-01 00:00:00 201005070300  1.97 213.55  1  0
> 2010-05-01 00:00:00 201005070600  1.20 199.53  1  0
> 2010-05-01 00:00:00 201005070900  1.87 102.46  1  0
> 2010-05-01 00:00:00 201005071200  1.89 131.60  1  0
> 2010-05-01 00:00:00 201005071500  2.24 195.08  1  0
> 2010-05-01 00:00:00 201005071800  1.66 228.50  1  0
> 2010-05-01 00:00:00 201005072100  1.84 219.40  1  0
> 2010-05-01 00:00:00 201005080000  1.90 210.57  1  0
> 2010-05-01 00:00:00 201005080300  2.89 228.87  1  0
> 2010-05-01 00:00:00 201005080600  1.38 241.61  1  0
> 2010-05-01 00:00:00 201005080900  0.73 171.06  1  0
> 2010-05-01 00:00:00 201005081200  2.22 199.67  1  0
> 2010-05-01 06:00:00 201005010600  1.98  58.86  1  0
> 2010-05-01 06:00:00 201005010900  0.54  93.93  1  0
> 2010-05-01 06:00:00 201005011200  2.45 128.41  1  0
> 2010-05-01 06:00:00 201005011500  2.58 174.05  1  0
> 2010-05-01 06:00:00 201005011800  1.65 194.73  1  0
> 2010-05-01 06:00:00 201005012100  0.93 256.41  1  0
> 2010-05-01 06:00:00 201005020000  1.00 235.41  1  0
> 2010-05-01 06:00:00 201005020300  1.94 269.23  1  0
> 2010-05-01 06:00:00 201005020600  1.88   2.23  1  0
> 2010-05-01 06:00:00 201005020900  1.26  82.13  1  0
> 2010-05-01 06:00:00 201005021200  2.10 127.77  1  0
> 2010-05-01 06:00:00 201005021500  3.55 123.00  1  0
> 2010-05-01 06:00:00 201005021800  1.48 154.10  1  0
> 2010-05-01 06:00:00 201005022100  1.33 207.58  1  0
> 2010-05-01 06:00:00 201005030000  1.20 218.35  1  0
> 2010-05-01 06:00:00 201005030300  1.06 237.34  1  0
> 2010-05-01 06:00:00 201005030600  0.99  23.57  1  0
> 2010-05-01 06:00:00 201005030900  1.89  91.52  1  0
> 2010-05-01 06:00:00 201005031200  3.06 113.00  1  0
> 2010-05-01 06:00:00 201005031500  3.55 141.46  1  0
> 2010-05-01 06:00:00 201005031800  3.02 200.50  1  0
> 2010-05-01 06:00:00 201005032100  2.16 225.42  1  0
> 2010-05-01 06:00:00 201005040000  1.49 243.19  1  0
> 2010-05-01 06:00:00 201005040300  1.88 285.39  1  0
> 2010-05-01 06:00:00 201005040600  1.56  35.71  1  0
> 2010-05-01 06:00:00 201005040900  1.85  78.84  1  0
> 2010-05-01 06:00:00 201005041200  1.99  96.05  1  0
> 2010-05-01 06:00:00 201005041500  0.96 217.58  1  0
> 2010-05-01 06:00:00 201005041800  1.74 218.54  1  0
> 2010-05-01 06:00:00 201005042100  1.49 238.49  1  0
> 2010-05-01 06:00:00 201005050000  1.63 276.95  1  0
> 2010-05-01 06:00:00 201005050300  2.63 293.77  1  0
> 2010-05-01 06:00:00 201005050600  1.54 338.63  1  0
> 2010-05-01 06:00:00 201005050900  1.39  36.10  1  0
> 2010-05-01 06:00:00 201005051200  0.63  63.33  1  0
> 2010-05-01 06:00:00 201005051500  1.19 178.10  1  0
> 2010-05-01 06:00:00 201005051800  2.14 196.01  1  0
> 2010-05-01 06:00:00 201005052100  2.24 237.81  1  0
> 2010-05-01 06:00:00 201005060000  1.80 273.62  1  0
> 2010-05-01 06:00:00 201005060300  2.52 266.71  1  0
> 2010-05-01 06:00:00 201005060600  0.91 245.52  1  0
> 2010-05-01 06:00:00 201005060900  1.27 149.67  1  0
> 2010-05-01 06:00:00 201005061200  2.25 146.56  1  0
> 2010-05-01 06:00:00 201005061500  1.63 115.76  1  0
> 2010-05-01 06:00:00 201005061800  2.76 204.32  1  0
> 2010-05-01 06:00:00 201005062100  1.35 248.81  1  0
> 2010-05-01 06:00:00 201005070000  1.21 220.91  1  0
> 2010-05-01 06:00:00 201005070300  1.79 236.16  1  0
> 2010-05-01 06:00:00 201005070600  0.92 161.52  1  0
> 2010-05-01 06:00:00 201005070900  1.83 124.90  1  0
> 2010-05-01 06:00:00 201005071200  2.99 153.84  1  0
> 2010-05-01 06:00:00 201005071500  2.65 189.17  1  0
> 2010-05-01 06:00:00 201005071800  2.20 202.54  1  0
> 2010-05-01 06:00:00 201005072100  1.81 195.49  1  0
> 2010-05-01 06:00:00 201005080000  1.64 218.71  1  0
> 2010-05-01 06:00:00 201005080300  2.44 233.86  1  0
> 2010-05-01 06:00:00 201005080600  0.96 194.43  1  0
> 2010-05-01 06:00:00 201005080900  1.76 138.89  1  0
> 2010-05-01 06:00:00 201005081200  2.79 146.98  1  0
> 2010-05-01 06:00:00 201005081500  3.45 226.90  1  0
> 2010-05-01 06:00:00 201005081800  2.56 263.20  1  0
> 2010-05-01 12:00:00 201005011200  2.56 148.69  1  0
> 2010-05-01 12:00:00 201005011500  2.54 172.28  1  0
> 2010-05-01 12:00:00 201005011800  1.78 195.38  1  0
> 2010-05-01 12:00:00 201005012100  1.23 259.67  1  0
> 2010-05-01 12:00:00 201005020000  1.04 237.58  1  0
> 2010-05-01 12:00:00 201005020300  1.75 265.84  1  0
>
> Regards,
> Shankar
>
> ________________________________
> From: R. Michael Weylandt <michael.weylandt at gmail.com>
> To: David Winsemius <dwinsemius at comcast.net>
> Cc: abcdef ghijk <linehunk at yahoo.com>; "r-help at r-project.org"
> <r-help at r-project.org>
> Sent: Wednesday, 16 November 2011, 18:26
> Subject: Re: [R] Interpolating hourly basis
>
> Can you provide a dput() example of your data and put it in a workable
> form -- I have no idea what that long list of numbers nor the 1 / 0 on
> the end represent. I think if you do this with a real time series
> class like xts you can create a new object with the interpolated
> times; fill in the values you know and leave the rest as NAs and then
> use a variety of NA interpolation options.
>
> Michael
>
> On Wed, Nov 16, 2011 at 9:56 AM, David Winsemius <dwinsemius at comcast.net>
> wrote:
>>
>> On Nov 16, 2011, at 8:56 AM, abcdef ghijk wrote:
>>
>>> I have a huge data set in the form of
>>>           V1           V2         V3   V4     V5  V6
>>> 1     201005010000201005010000 1.68291.38 1  0
>>> 2     201005010000201005010300 0.93335.10 1  0
>>> 3     201005010000201005010600 2.25 57.38 1  0
>>> 4     201005010000201005010900 0.43 13.76 1  0
>>> 5     201005010000201005011200 0.74101.14 1  0
>>>
>>> I am interested in interpolating it on an hour basis(it's for avery 3
>>> hours). Data is also being updated after every six hours for next eight
>>> days.
>>
>> Please don't cross post on R help and StackOverflow.
>>
>> --
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> 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.
>>
>
>



More information about the R-help mailing list