RJLewis
2009-Nov-17 13:01 UTC
[R] Re place NA values in matrix with the value of Nearest Neighbour
I am extracting climate data for coastal areas from 5km grid data for specific xy coordinates that relates to individual study sites (SQ). Code so far: setwd("/Users/roblewis/Documents/PhD/Climate Data") fnamestemp=read.table("Path_Directory_Maxt") fnames=paste(fnamestemp[,1],fnamestemp[,2]) nfiles = length(fnames) xy=as.matrix(read.table("xy_.txt")) sq<-xy[,1] x<-xy[,2] y<-xy[,3] xy<-cbind(x,y) xy<-xy/5000 xy<- xy+41 xy<- cbind(xy[,2],xy[,1]) nfiles=2 for( imap in 2:nfiles) { print(fnames[imap]) x = as.matrix(read.table(fnames[imap],skip=6)) x[x = -99999] = NA x=flipud(x) siteclim <- x[ xy] } This all works fine, however as many of the sites are so close to the sea, many NA's are returned, as teh climate information has not been extrapolated to include detailed coastal areas. Thus i would like to know how i can modify my script to instruct R to replace the NA values of 'siteclim' with the value of its nearest neighbour in the 5km grid data on the condition that this is also not NULL. Any suggestions would be gratefully received Thanks -- View this message in context: http://old.nabble.com/Replace-NA-values-in-matrix-with-the-value-of-Nearest-Neighbour-tp26389519p26389519.html Sent from the R help mailing list archive at Nabble.com.