R/fillbynearest.R

Defines functions fillbynearest

fillbynearest <- function(adjacency=NULL,
                          covariate=NULL) {

  # get list of shortest paths
  mygraph <- graph.adjacency(adjmatrix=listw2mat(adjacency),
                             mode="undirected")
  mypaths <- shortest.paths(graph=mygraph)

  # figure out which of the covariates are NA
  whichcovsNA <- which(is.na(covariate))

  # set path lengths to infinite to nodes which are NA
  mypaths[,whichcovsNA] <- Inf

  nearestnonNA <- rep(0, length(covariate))
  # for each row, figure out which non-NA node is nearest
  for (i in 1:length(nearestnonNA)) {

    nearestnonNA[i] <- which(mypaths[i,] == min(mypaths[i,]))[1]

  }
  return(unlist(covariate[nearestnonNA], use.names=FALSE))

}
16charan08/gaffar documentation built on Sept. 11, 2020, 8:40 p.m.