Reputation: 123
I want R to know that this stack NetCDF contains a value -1e30 which is a fill value and it's a NA. Since the other actual values are quite similar to the NA value (1e17 to -4e18).R should understand that -1e30 is an NA and skips NA in further analysis for code na.rm=TRUE
.
I am working on list of files in NetCDF DATA https://drive.google.com/open?id=14OX9JAe7oZ7xPRuIzR7YB2P3OHC3zjJU
> ras <- list.files("filepath", pattern = "\\.nc$", full=TRUE)
> Data <- stack(ras)
> u2 <- mean(Data, na.rm=TRUE).
How do I write the code?
> NA<- -1e30
> Mean <-mean(Data,na.rm=TRUE)
or
> Data[Data < -1e30] = NA
> Mean <-mean(Data,na.rm=TRUE)
Will this code make R understand that -1e30 is an NA in the dataset and skip the -1e30 in taking out mean?
> dput(Data[1:100, 1:100])
NaN, 4748499736330240, NA, NA, NA, -2119029758099456, -1656679481475072, 3074375171440640, 2699225347391488, 1389911546527744, 3767667181748224,..
Upvotes: 0
Views: 543
Reputation: 101383
Maybe you should used |
for condition in ifelse()
r <- mean(ifelse(Data < -1e30 | is.nan(Data), NA, Data),na.rm = T)
Example with dummy data
Data <- c(NaN, 4748499736330240, NA, NA, NA, -2119029758099456, -1656679481475072, 3074375171440640, 2699225347391488, 1389911546527744, 3767667181748224, -1e31)
r <- mean(ifelse(Data < -1e30 | is.nan(Data), NA, Data),na.rm = T)
such that
> r
[1] 1.700567e+15
where
> ifelse(Data < -1e30 | is.nan(Data), NA, Data)
[1] NA 4.748500e+15 NA NA
[5] NA -2.119030e+15 -1.656679e+15 3.074375e+15
[9] 2.699225e+15 1.389912e+15 3.767667e+15 NA
Upvotes: 0
Reputation: 8117
You have a vector
a <- c(1,2,3,1e30)
You need to replace the values in there:
a <- ifelse(a == 1e30, NA, a)
Upvotes: 2