[R] Filling in missing time samples with na.approx
Jason Edgecombe
jason at rampaginggeek.com
Mon Nov 29 19:33:13 CET 2010
On 11/29/2010 10:00 AM, Gabor Grothendieck wrote:
> On Mon, Nov 29, 2010 at 9:45 AM, Jason Edgecombe
> <jason at rampaginggeek.com> wrote:
>
>> Hi Everyone,
>>
>> I have a some data from a sports gps device like the following:
>>
>> time latitude longitude altitude distance heartrate
>> 1 1277648884 0.304048 -0.793819 260 0.000000 94
>> 2 1277648885 0.304056 -0.793772 262 4.307615 95
>> 3 1277648888 0.304060 -0.793696 263 11.262347 97
>> 4 1277648894 0.304075 -0.793544 263 25.237911 103
>> 5 1277648898 0.304085 -0.793455 263 33.322525 108
>> 6 1277648902 0.304064 -0.793387 256 40.042988 115
>>
>> As you can see, the samples have irregular holes in the time column. How can
>> I fill in the missing samples using na.approx?
>>
>> I've tried to creating a blank series with no gaps and combine them, but
>> "merge" just adds columns and "rbind" compains about duplicate indexes.
>>
>> P.S. My GPS still has holes in the data when I turn off "smart recording" :(
>>
>>
> Try this:
>
> Lines<- "time latitude longitude altitude distance heartrate
> 1277648884 0.304048 -0.793819 260 0.000000 94
> 1277648885 0.304056 -0.793772 262 4.307615 95
> 1277648888 0.304060 -0.793696 263 11.262347 97
> 1277648894 0.304075 -0.793544 263 25.237911 103
> 1277648898 0.304085 -0.793455 263 33.322525 108
> 1277648902 0.304064 -0.793387 256 40.042988 115"
>
> # read in data
> library(zoo)
> z<- read.zoo(textConnection(Lines), header = TRUE)
>
> na.approx(z, xout = seq(min(time(z)), max(time(z))))
>
>
>
>
No change:
> na.approx(z, xout = seq(min(time(z)), max(time(z))))
latitude longitude altitude distance heartrate
1277648884 0.304048 -0.793819 260 0.000000 94
1277648885 0.304056 -0.793772 262 4.307615 95
1277648888 0.304060 -0.793696 263 11.262347 97
1277648894 0.304075 -0.793544 263 25.237911 103
1277648898 0.304085 -0.793455 263 33.322525 108
1277648902 0.304064 -0.793387 256 40.042988 115
There should be 19 samples after the na.approx.
I'm guessing that na.approx is what I need, but I'm open to suggestions.
Thanks,
Jason
More information about the R-help
mailing list