0

I am working with globally gridded data of annual maximum precipitation. However, I want to isolate those maximum value for land areas "only" for each of my 145 years by using a mask (so 145 maximum values based on all land areas). That said, I am receiving only NA values when I apply the mask, and I cannot understand why (when the mask is not applied, the below procedure works just fine). Here is what I have done so far:

Model66 <- brick("MaxPrecNOAA-GFDLGFDL-ESM2Ghistorical.nc", var="onedaymax")



#Applying the mask to isolate land areas only:

data("wrld_simpl")
b <- wrld_simpl
land <- mask(Model66,b)

#To derive highest maximum value for each layer/year for land only (145 years = 145 maximum values)

Gmax <- sapply(unstack(land), function(r){max(values(r))}) 

Gmax
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 
NA NA NA NA NA NA NA
[40] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 
NA NA NA NA NA NA NA
[79] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 
NA NA NA NA NA NA NA
[118] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

Why would this be happening? I isolated land only, and my plots correctly show that the mask worked, as only land has values on the plots for each layer/year (and the idea would be take the highest value among these for each layer/year, as I attempted to do with object "Gmax"). Again, when a mask is not applied, NAs don't show up, so I wonder if it is just a small detail causing this when using the mask?

Any help with this would be greatly appreciated!

Thanks!

Rain1290
  • 35
  • 7
  • If you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). you could make it easier for others to find and test a answer to your question. That way you can help others to help you! – dario Mar 09 '20 at 14:54

1 Answers1

1

Try with:

Gmax <- sapply(unstack(land), function(r){max(values(r), na.rm=T)}) 

Your NAs are considered by R like the maximum value (positive infinitum), you can disable that option with na.rm=TRUE

  • That really did the trick!!!! Thank you, Armando! It really was such a small detail causing all that! Thank you so much!!! – Rain1290 Mar 09 '20 at 16:06