cpuguru
cpuguru

Reputation: 3803

Filling in Missing GPS Values in R

I am looking at data from a device that spends much of its time underwater. When it surfaces, it gets a GPS fix (lat & lon) and then sinks (losing its GPS fix) and continues gathering data again until its next surfacing. This results in a lot of NA values in the data for the lat lon values and they don't coincide with the underwater data readings.

I'd like to create a curtain plot of the data but I'll need some interpolated lat/lon values to do a rough plot of the subsurface data against for a 3D map.

How can I fill in some linearly interpolated values for the many NA's that occur in between GPS fixes in R? They are in numeric DD.DDDDD format.

An example of the data can be found at: http://modata.ceoe.udel.edu/public/gps_example_data.csv

Upvotes: 4

Views: 1059

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162441

na.approx() in the zoo package does just what you're looking for.

With your data, do something like:

df <- read.csv("http://modata.ceoe.udel.edu/public/gps_example_data.csv", 
               header=T)

library(zoo)

df2 <- 
transform(df, 
    m_gps_lat_dec = na.approx(m_gps_lat_dec, m_present_time, na.rm=FALSE),
    m_gps_lon_dec = na.approx(m_gps_lon_dec, m_present_time, na.rm=FALSE))

See also this StackOverflow post, which includes a nice worked example (and some useful comments from an author of the zoo package, to boot.)

Upvotes: 4

Related Questions