0

I'm trying to extract pixel value using the extract() function from raster package in R and get the following error, bellow in my code bellow a well, where: df$PixelNumber is the "cell number value". The error :

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"character", "numeric"’

the code I'm using:

vec=as.vector(as.numeric(as.matrix(df$PixelNumber)));
rast = file.choose();
VlaueSlope=extract(x= rast, vec)

What could I be doing wrong in the extract() code that cannot get the pixel value in the variable VlaueSlope.

  • Could you try vec = as.numeric(as.matrix(df$PixelNumber))) ? – JonasV Mar 28 at 11:13
2

The error:

    Error in (function (classes, fdef, mtable) : 
     unable to find an inherited method for
     function ‘extract’ for signature ‘"character", "numeric"’

is saying you are calling extract with a "character" argument and a "numeric" argument. How are you calling it? Let's see...

VlaueSlope=extract(x= rast, vec)

so what is rast? Let's see:

rast = file.choose();

rast is the file name it is NOT the raster data. You need to read in the raster using something like:

rasterpath = file.choose()
rast = raster(rasterpath)

and then you can extract the pixel values if everything else is correct.

In summary: try and understand error messages, read the documentation for functions, and check your arguments match the ones documented.

| improve this answer | |
  • Yes, indeed I had has this mistake now works!!! Thanks!! how can In get the rather of only all (the cell original cell values of the raster) but only where the are points? thanks – Gab Mar 29 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.