[R] Filling in missing time samples with na.approx

Gabor Grothendieck ggrothendieck at gmail.com
Mon Nov 29 16:00:01 CET 2010


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))))



-- 
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