How to calculate average daily data in R?

I am working on climate change impacts on hydrology. I am using Rstudio for data analysis. I have a daily precipitation data (resolution 10km x 10km) for a catchment (catchment area is around 57000 Km^2) from 1981-2010 in NetCDF file. I want to calculate average daily data for the period 1981-2010 for each grid.

The NetCDF file has 1 variable (precipitation) and 3 dimensions (longitude, latitude and time).

Could you please suggest me scripts/functions that can be used for this purpose!

Thank you in advance.

Hi @Santosh_K,

this is very difficult to answer without a reprex. The typical pipeline would be

df %>%
group_by(time) %>%
summarize(avg_of_something = mean(something))

If you want to summarize all variables (except the group_by of course) then use summarize_all(mean).

Hello, and welcome to the RStudio Community Forum.

You need to do a bit of homework - remember "Google is your friend"!

Check out for following link for how to read netCDF files into R:

http://geog.uoregon.edu/bartlein/courses/geog607/Rmd/netCDF_01.htm

Once you have your data imported, you can set about calculating averages - I suggest you use the {dplyr} package. See this link for an introduction:

https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html

If you get stuck and want more detailed help you should work on making a "reprex" (reproducible example) including a small bit of your data and the R code that you have tried. Read up on this here:

https://www.tidyverse.org/help/

HTH

I don't think that this would work, as NetCDF files are not data frames but {raster} package raster objects.

I believe something like raster::cellStats or raster::calc should do the trick but I don't have a NetCDF file on hand right now...

Have a look at the documentation https://rspatial.org/spatial/8-rastermanip.html#calc

1 Like

I haven't worked with netCDF files ... but:
The documentation at
http://geog.uoregon.edu/bartlein/courses/geog607/Rmd/netCDF_01.htm
shows how a 'slice' of a raster 'brick' (3-D array) can be extracted into a dataframe suitable for processing and graphing with other tools (e.g. dplyr and ggplot). Presumably then, multiple slices can be 'stacked' into a long-form dataframe to capture all the information.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.