Error message: "[app] the number of values returned by 'fun' is not appropriate" for simple correlation

I have a problem with the following code for my simple correlation:

x_cor_1<-MW_1982_1996_T
y_cor_2<-NDVI_1997_2011_MW

corvec<- function(vec=NULL){cor(x= vec[1:(length(vec)/2)], y= vec[((length(vec)/2)+1):length(vec)], use = 'complete.obs', method='spearman')}
l_all<-rast(list(x_cor_1,y_cor_2))
corlyrs<- app(l_all, fun=function(x) {if(all(is.na(x))) {NA_real_} else {corvec(vec=x)}})

The error code always comes up: Fehler: [app] the number of values returned by 'fun' is not appropriate

--> x_cor_1: Mean value of temperature over 15 years (right side of picture)
--> y_cor_2: Mean value of NDVI over 15 years (Left side of picture)

Does anyone know where the error could be or has anyone ever had such an error message? Thanks a lot for your help!

Can you explain what that function corvec is supposed to do?

Why not just

cor(x_cor_1, y_cor_2)

cor()
doesn't work, because I am working with terra. I got this code from "https://www.researchgate.net/post/How_to_perform_Pearsons_correlation_between_two_sets_of_raster_images_that_represent_time_series_data" (Michael Kempf). This code is working with other data, but unfortunately not with mine. And I am not sure why the error code is appearing.

Kind regards,
Lisa Kloos

What is terra? Oh I see, it's an R package.

I had a look at the link, thanks. I have never worked with raster data so I am no sure how much help I can be but I think we need to see all of your code and a sample of your data. See FAQ: How to do a minimal reproducible example ( reprex ) for beginners for some suggestions.

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. I aw not sure if we need both the raster data and the csv data or just the csv data.

Michael's code is assuming a single vector (stack) but if I understand you correctly you are looking at two different vecters of data, so I do not understand why cor(x_cor_1, y_cor_2) will not work.

Please give us some sample data.

Seems the example is using a raster::stack() / RasterStack as input, whereas you are using a terra::rast / SpatRaster
I don't do geographical analysis myself often enough to tell you what to do to address, but I would look in that direction.

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.