I have an example 5x5 matrix with the following values:
dat <- matrix(seq(1,13,0.5), nrow=5, byrow=TRUE)
dat[seq(2,25,2)] <- NA
1 | NA | 2 | NA | 3
While this might have been asked before, I couldn't find a neat worked example of this circumstance. Try this, which will give a result you can then pass to persp
, image
etc...
#install.packages("akima")
library(akima)
nas <- !is.na(dat)
interp(
row(dat)[nas], #row index - 'x' values
col(dat)[nas], #col index - 'y' values
dat[nas], #height data - 'z' values
xo=seq(1,nrow(dat)), #'x' values for output
yo=seq(1,ncol(dat)) #'y' values for output
)
#$x
#[1] 1 2 3 4 5
#
#$y
#[1] 1 2 3 4 5
#
#$z
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1.0 1.5 2.0 2.5 3.0
#[2,] 3.5 4.0 4.5 5.0 5.5
#[3,] 6.0 6.5 7.0 7.5 8.0
#[4,] 8.5 9.0 9.5 10.0 10.5
#[5,] 11.0 11.5 12.0 12.5 13.0