R - Calculate mean of rasterbrick

Stijn

I am trying to calculate the mean of a rasterbrick over specific timesteps in R.

Subsetting the dataset shows a lay-out that appears to be correct (to me), however R does not allow any computations to follow up.

> r_sub <- b[[1699:1862]]
> r_sub
class      : RasterStack 
dimensions : 127, 147, 18669, 164  (nrow, ncol, ncell, nlayers)
resolution : 5, 5  (x, y)
extent     : 603299.4, 604034.4, 6615598, 6616233  (xmin, xmax, ymin, ymax)
crs        : NA 
names      : X2019.02.09.19, X2019.02.09.20, X2019.02.09.21, X2019.02.09.22, X2019.02.09.23, X2019.02.09.24, X2019.02.10.1, X2019.02.10.2, X2019.02.10.3, X2019.02.10.4, X2019.02.10.5, X2019.02.10.6, X2019.02.10.7, X2019.02.10.8, X2019.02.10.9, ... 

> r_mean <- calc(r_sub, mean)
Error in Rsx_nc4_get_vara_double: NetCDF: Index exceeds dimension bound
Var: SWIT  Ndims: 3   Start: 1698,0,0 Count: 1,127,147
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset,  : 
  C function R_nc4_get_vara_double returned error

How to solve this specific error?

Robert Hijmans

I am guessing that this is related to a problem with your file, not with the code.

I did this and it worked well

library(raster)
b <- brick("filename.nc")
r_sub <- b[[1699:1862]]
r_sub
#class      : RasterStack 
#dimensions : 160, 320, 51200, 164  (nrow, ncol, ncell, nlayers)
#resolution : 1.125, 1.121277  (x, y)
#extent     : -0.5625, 359.4375, -89.70216, 89.70216  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

mean(r_sub)
#class      : RasterLayer 
#dimensions : 160, 320, 51200  (nrow, ncol, ncell)
#resolution : 1.125, 1.121277  (x, y)
#extent     : -0.5625, 359.4375, -89.70216, 89.70216  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#source     : memory
#names      : layer 
#values     : 3.033032e-07, 0.0002482847  (min, max)

Also, you might want to look into using stackApply

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculate a mean, by a condition, within a factor [r]

From Dev

r search along a vector and calculate the mean

From Dev

in R, how to calculate mean of all column, by group?

From Dev

for loop in R to calculate mean from list

From Dev

Nested loops in R to calculate a mean day

From Dev

How to calculate mean of two timestamp columns in R?

From Dev

calculate grand mean from means in r

From Dev

Calculate the mean of a row excluding certain columns in R

From Dev

How to calculate mean for all pairwise combinations in R

From Dev

Calculate mean based on marker in a second column in R

From Dev

How To Calculate Mean In R (Data Structure)

From Dev

R: calculate the row mean in a matrix

From Dev

Calculate a lag or lead mean in r

From Dev

Calculate min, maximum and mean in R

From Dev

Calculate the mean value of symmetry data in a matrix with R

From Dev

Calculate Mean of Multiply Columns with Condition in R

From Dev

R:how to calculate poblational standard deviation and mean

From Dev

How to calculate mean points for elements in a List in R

From Dev

Calculate daily mean of data frame in r

From Dev

R program to calculate the mean of the results of the same experiments

From Dev

calculate logarithm and mean, add the result to dataset in R

From Dev

Calculate mean of dataset in R that satisfies two conditions

From Dev

Running a for loop in r over columns to calculate mean

From Dev

How to Calculate eCDF Mean in MatchIt() R

From Dev

R Loop to calculate mean for each of two variables

From Dev

Subsetting a rasterbrick to give mean of three minimum months in each year

From Dev

R calculate how many values used to calculate mean in aggregate function

From Dev

Calculate sd and mean for many data frames in R

From Dev

Using 'boot' to calculate bootstrapped mean in R

Related Related

  1. 1

    Calculate a mean, by a condition, within a factor [r]

  2. 2

    r search along a vector and calculate the mean

  3. 3

    in R, how to calculate mean of all column, by group?

  4. 4

    for loop in R to calculate mean from list

  5. 5

    Nested loops in R to calculate a mean day

  6. 6

    How to calculate mean of two timestamp columns in R?

  7. 7

    calculate grand mean from means in r

  8. 8

    Calculate the mean of a row excluding certain columns in R

  9. 9

    How to calculate mean for all pairwise combinations in R

  10. 10

    Calculate mean based on marker in a second column in R

  11. 11

    How To Calculate Mean In R (Data Structure)

  12. 12

    R: calculate the row mean in a matrix

  13. 13

    Calculate a lag or lead mean in r

  14. 14

    Calculate min, maximum and mean in R

  15. 15

    Calculate the mean value of symmetry data in a matrix with R

  16. 16

    Calculate Mean of Multiply Columns with Condition in R

  17. 17

    R:how to calculate poblational standard deviation and mean

  18. 18

    How to calculate mean points for elements in a List in R

  19. 19

    Calculate daily mean of data frame in r

  20. 20

    R program to calculate the mean of the results of the same experiments

  21. 21

    calculate logarithm and mean, add the result to dataset in R

  22. 22

    Calculate mean of dataset in R that satisfies two conditions

  23. 23

    Running a for loop in r over columns to calculate mean

  24. 24

    How to Calculate eCDF Mean in MatchIt() R

  25. 25

    R Loop to calculate mean for each of two variables

  26. 26

    Subsetting a rasterbrick to give mean of three minimum months in each year

  27. 27

    R calculate how many values used to calculate mean in aggregate function

  28. 28

    Calculate sd and mean for many data frames in R

  29. 29

    Using 'boot' to calculate bootstrapped mean in R

HotTag

Archive