[R] Interpolation Problems
Duncan Murdoch
murdoch at stats.uwo.ca
Tue Sep 2 00:45:35 CEST 2008
On 01/09/2008 6:17 PM, Steve Murray wrote:
> Dear all,
>
> I'm trying to interpolate a dataset to give it twice as many values (I'm giving the dataset a finer resolution by interpolating from 1 degree to 0.5 degrees) to match that of a corresponding dataset.
>
> I have the data in both a data frame format (longitude column header values along the top with latitude row header values down the side) or column format (in the format latitude, longitude, value).
>
> I have used Google to determine 'approxfun' the most appropriate command to use for this purpose - I may well be wrong here though! Nevertheless, I've tried using it with the default arguments for the data frame (i.e. interp <- approxfun(dataset) ) but encounter the following errors:
>
>> interp <- approxfun(JanAv)
> Error in approxfun(JanAv) :
> need at least two non-NA values to interpolate
> In addition: Warning message:
> In approxfun(JanAv) : collapsing to unique 'x' values
>
>
> However, there are no NA values! And to double-check this, I did the following:
>
>> JanAv[is.na(JanAv)] <- 0
>
> ...to ensure that there really are no NAs, but receive the same error message each time.
>
> With regard to the latter 'collapsing to unique 'x' values', I'm not sure what this means exactly, or how to deal with it.
>
>
> Any words of wisdom on how I should go about this, or whether I should use an alternative command (I want to perform a simple (e.g. linear) interpolation), would be much appreciated.
What is JanAv? approxfun needs to be able to construct x and y values
to interpolate; it may be that your JanAv object doesn't allow it to do
that. (The general idea is that it will consider y to be a function of
x, and will construct a function that takes arbitrary x values and
returns y values matching those in the dataset, with some sort of
interpolation between values.)
If you really have longitude and latitude on some sort of grid, you
probably want a two-dimensional interpolation, not a 1-d interpolation
as done by approxfun. The interp() function in the akima() package does
this, but maybe not in the format you need.
Duncan Murdoch
More information about the R-help
mailing list