Paired correlation between raster layers in R

I need to perform Pearson pair correlation between 19 raster layers for the continents of Africa extracted from the WordClim database. I want to check which levels of variables are more correlated / significant for my model. To do this, I tried to use the layerStats function from the raster package, but after execution, my output does not contain numeric values, all rows and columns displayed NA values. Below is my script.

#Loading raster files from WorldClim database
rastFiles<- list.files(pattern="bil")
a<-stack(rastFiles)

# Adjusting for African Continent
newext<-c(-20, 55, -35, 45)
Africa<-crop(a,newext)
Africa

#Correlation
cor<-layerStats(Africa,'pearson')
+4
source share
1 answer

In r, simply use the code below, ensuring that na.rm = T deals with HC by layers:

library(raster)    
jnk=layerStats(raster_stack, 'pearson', na.rm=T)
corr_matrix=jnk$'pearson correlation coefficient'
+3
source

All Articles