1

I am trying to smoothen my raster stack using Savitzky-Golay method. I am following the workaround from HERE but my problem is setting start and end of my time-series raster. My series of images were from October 2018 to March 2019 and can available HERE.

My code goes like this:

r <- stack("./KT_DS18_rice_ts.tif")
names(r) <- c("2018-10-08","2018-10-24","2018-11-09","2018-11-25",
                      "2018-12-11","2018-12-27","2019-01-09","2019-01-25",
                      "2019-02-10","2019-02-26","2019-03-14","2019-03-30")

fun <- function(x) {
  v=as.vector(x)
  z=na.spline(v)
  s1.ts2 = ts(z, start=1, end=nlayers(z), frequency=12)
  sgolayfilt(s1.ts2)  
}

fun(r)

I want to smooth the EVI curve of each pixel from the first layer (start) to the 12th layer (end). That code above gives me this error:

 Error in (function (classes, fdef, mtable):unable to find an inherited method for function ‘nlayers’ for signature ‘"numeric"’ 
  • Please make your questions as self-contained as possible - how about creating a simple sample data set so we don't have to download? And say all the packages you use so we don't have to go hunting to discover you are using the signal package. Help us to help you. – Spacedman Aug 17 '19 at 13:06
1

Note where the error is coming from and step through your code. Its not even getting to the Savitsky-Golay line.

> v = as.vector(r)
> z = na.spline(v)
> s1.ts2 = ts(z, start=1, end=nlayers(z), frequency=12)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘nlayers’ for signature ‘"numeric"’

The error is clearly with nlayers. What are you calling it on?

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

z is the vector of values from your raster stack. It doesn't have "layers" so nlayers fails. Did you mean nlayers(r) to get the value 12?

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.