knnLookup function - preparing the data

I am trying to use the function knnLookup in order to search for neighboring blocks using a data.frame. I am following this code:

# Neighboring block search
Tree <- createTree(pcoarsegrid@coords)
Newdat <- matrix(pcoarsegrid@coords[i,], ncol= 2)
Block.loc <- as.numeric(knnLookup(Tree, newdat= Newdat, k = nmax))

which can be found here.
When I run the code in the second line I get the following error:

Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'i' in selecting a method for function '[': object 'i' not found

Can you please help me solve it? Here is my code:

ntl = raster("ntl.tif")

#Get the coordinates of the cell centres and create a Spatial object: 
spts <- rasterToPoints(ntl, spatial = TRUE)

x <- as.data.frame(spts)
#drop column named ntl
xcoords = subset(x, select = -c(ntl))

# Neighboring block search
Tree <- createTree(xcoords)
Newdat <- matrix(Tree[i,], ncol = 2)
Block.loc <- as.numeric(knnLookup(Tree, newdat = Newdat, k = nmax))

My data:

new("RasterLayer", file = new(".RasterFile", name = "C:\\Users\\Geography\\Desktop\\r4atprk\\ntl.tif", 
    datanotation = "FLT4S", byteorder = "little", nodatavalue = -Inf, 
    NAchanged = FALSE, nbands = 1L, bandorder = "BIL", offset = 0L, 
    toptobottom = TRUE, blockrows = 128L, blockcols = 128L, driver = "gdal", 
    open = FALSE), data = new(".SingleLayerData", values = logical(0), 
    offset = 0, gain = 1, inmemory = FALSE, fromdisk = TRUE, 
    isfactor = FALSE, attributes = list(), haveminmax = TRUE, 
    min = 1.1743594408035, max = 163.47160339355, band = 1L, 
    unit = "", names = "ntl"), legend = new(".RasterLegend", 
    type = character(0), values = logical(0), color = logical(0), 
    names = logical(0), colortable = logical(0)), title = character(0), 
    extent = new("Extent", xmin = 3639104.5743, xmax = 3827654.5743, 
        ymin = 4398413.7145, ymax = 4581563.7145), rotated = FALSE, 
    rotation = new(".Rotation", geotrans = numeric(0), transfun = function () 
    NULL), ncols = 419L, nrows = 407L, crs = new("CRS", projargs = "+proj=lcc +lat_0=24 +lon_0=80 +lat_1=12.2822638 +lat_2=35.1022096 +x_0=4000000 +y_0=4000000 +datum=WGS84 +units=m +no_defs"), 
    history = list(), z = list())

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.