0

I am basic in R and this code should be working although I get an error on this part of the code. Although I tried adding a "simple" method it seems not to solve the issue

library(terra)
# create SpatRast for all SM files
imgSM <- rast(sub("NDVI_cropped.tif","SM_cropped.tif",files))
xy <- cbind(x,y)

valNDVI <- extract(img, xy)0.0001 # extract and convert DN to NDVI
#Error: unexpected numeric constant in "valNDVI <- extract(img, xy)0.0001"

valSM <- extract(imgSM, xy) # extract SM
#Error in UseMethod("extract") : 
#  no applicable method for 'extract' applied to an object of class "SpatRaster"
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • It looks like you're new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput(head(dataObject))` and any libraries you are using. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Jun 14 '22 at 18:08

1 Answers1

2

This error

valNDVI <- extract(img, xy)0.0001 
#Error: unexpected numeric constant in "valNDVI <- extract(img, xy)0.0001"

Is probably obvious when you see it. It should be without the trailing 0.0001

valNDVI <- extract(img, xy)

This one

valSM <- extract(imgSM, xy)
#Error in UseMethod("extract") : 
#  no applicable method for 'extract' applied to an object of class"SpatRaster"

Suggests that you have loaded another package that has an extract methods and masks the method from terra (probably the tidyr package). It is good practice to only load packages you need (avoid library(tidyverse)), and you can also prepend the package name to avoid conflicts. Like this:

valSM <- terra::extract(imgSM, xy)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63