How to calculate mean rainfall of each lat long for 20 years

Viewed 18

I have a massive dataset of rainfall with 1048575 rows and four columns (time, lat, long, rainfall in mm). The dataset contains daily rainfall for 20 years for each unique lat-long combination (a total of 81 such lat-long combinations). I want to calculate the mean rainfall of 20 years for each unique lat-long combination.

I am new to R, Can anybody help me with the code in R?

I am giving the code I have prepared and a glimpse of the dataset

library(dplyr)
data <-read.csv('IMD_Precip.csv')
data$rain[data$rain==-999]<-NA

data_latlon <-data.frame(Lat =data$lat,
                     Long = data$lon,
                     PlotCode = data$rain,
                     date=as.POSIXct(data$time, "%Y %m %d"))
data_latlon %>% 
  group_by(date, Lat, Long) %>%
  mean(PlotCodeGrouped =paste(PlotCode, date, collapse = ''))

enter image description here

1 Answers

If you want the anual mean rain try this:

 data_latlon %>% 
      mutate(year = lubridate::year(date)) %>%
      group_by(year, Lat, Long) %>%
      summarise(mean = mean(PlotCode, na.rm = T))

If u want the mean of all data, this:

data_latlon %>% 
      group_by(Lat, Long) %>%
      summarise(mean = mean(PlotCode, na.rm = T))
Related