When analyzing scientific data, such as temperature, rainfall or humidity, you may find the data in a NetCDF format (.nc). This post will describe how to convert it to a data frame format readable in R, and then filter its latitude and longitude values over a polygon shapefile.
I am using the 2019 rainfall dataset provided by SILO, which you can download here. This dataset contains daily information about rainfall over Australia for the year 2019.
As a polygon shapefile to filter the rainfall data, I am using the Victoria State Polygon shapefile, which can be downloaded here
First, I will read the raifall dataset in NetCDF format. Later, I will convert it to a data frame format in R, which can be exported to CSV. Finally, I will filter the rainfall data to Victoria state boundaries using latitude and longitude, excluding other Australian states and then plot the mean rainfall over Victoria
First, download the datasets provided in the Datasets section
We will use below libraries, so make sure to install them:
First of all, we need to read the NetCDF file using the nc_open function
#Reading file
daily_rain <- "2019" #file name
ncfname <- paste(daily_rain, ".nc", sep = "")
dname <- "daily_rain"
ncin <- nc_open(ncfname) #opening NetCDF file
print(ncin) #print basic information about the file
## File 2019.nc (NC_FORMAT_NETCDF4):
##
## 2 variables (excluding dimension variables):
## short daily_rain[lon,lat,time] (Chunking: [841,681,1])
## _FillValue: -32767
## long_name: Daily rainfall
## units: mm
## scale_factor: 0.1
## add_offset: 0
## char crs[] (Contiguous storage)
## long_name: Coordinate reference system
## grid_mapping_name: latitude_longitude
## longitude_of_prime_meridian: 0
## semi_major_axis: 6378137
## inverse_flattening: 298.257223563
##
## 3 dimensions:
## lat Size:681
## long_name: latitude
## standard_name: latitude
## units: degrees_north
## axis: Y
## lon Size:841
## long_name: longitude
## standard_name: longitude
## units: degrees_east
## axis: X
## time Size:283 *** is unlimited ***
## calendar: standard
## axis: T
## units: days since 2019-01-01
##
## 10 global attributes:
## _NCProperties: version=1|netcdflibversion=4.6.1|hdf5libversion=1.10.2
## department: Department of Environment and Science
## department_short: DES
## copyright: Copyright - the State of Queensland Department of Environment and Science 2018
## site_url: http://silo.longpaddock.qld.gov.au
## institution: Queensland Government, Department of Environment and Science
## raster_source: Gridded surface was created by interpolating observational data provided by the Australian Bureau of Meteorology.
##
## metadata_url: http://qldspatial.information.qld.gov.au/catalogueadmin/catalog/main/home.page
## reference: Jeffrey, S.J., Carter, J.O., Moodie, K.B.A. and Beswick, A.R.. "Using spatial interpolation to construct a comprehensive archive of Australian climate data", Environmental Modelling and Software, Vol 16/4, pp 309-330, 2001
##
## disclaimer: 1. The user accepts all responsibility and risks associated with the use of this data.
## 2. The Queensland Government makes no representations or warranties in relation to this data, and, you agree that to the extent
## permitted by law, all warranties relating to accuracy, reliability, completeness, currency or suitability for any
## particular purpose and all liability for any loss, damage or costs (including consequential damage) incurred in any
## way (including but not limited to that arising from negligence) in connection with any use of or reliance on this
## data are excluded or limited.
## 3. You agree to continually indemnify the State of Queensland (and its officers and employees) against
## any loss, cost, expense, damage and liability of any kind (including liability in negligence) caused by your use of
## this data or any product made from this data.
We will need to get the longitude values contained in the NetCDF file. There are two ways of doing it. The first way is to get all longitude values over Australia
#Getting longitude values
lon <- ncvar_get(ncin, "lon")
nlon <- dim(lon)
head(lon)
## [1] 112.00 112.05 112.10 112.15 112.20 112.25
The second way is to get the longitude values within a range. As we want to filter over Victoria state, we don’t need longitude values outside its range.
# Getting the longitude indexes which longitude values are between 140.9 and 149.8 (Victoria State)
lonIdx <- which( ncin$dim$lon$vals > 140.9 & ncin$dim$lon$vals < 149.8) ##
# Reading longitude values within longitute indexes range (lonIdx)
lon <- ncvar_get(ncin, "lon", start=c(min(lonIdx)), count=c((max(lonIdx)-min(lonIdx)+1)))
nlon <- dim(lon)
head(lon)
## [1] 140.95 141.00 141.05 141.10 141.15 141.20
Same for latitude. We could get all possible latitude values for Australia
#Getting latitude values
lat <- ncvar_get(ncin, "lat", verbose = F)
nlat <- dim(lat)
head(lat)
## [1] -44.00 -43.95 -43.90 -43.85 -43.80 -43.75
However, to optimize memory and computational resources, we are reading latitude values within Victoria state range.
# Getting the latitude indexes which latitude values are between -39.10 and -34 (Victoria State)
latIdx <- which( ncin$dim$lat$vals > -39.10 & ncin$dim$lat$vals < -34)
# Reading latitude values within this index
lat <- ncvar_get(ncin, "lat", verbose = F, start=c(min(latIdx)), count=c((max(latIdx)-min(latIdx)+1)))
nlat <- dim(lat)
head(lat)
## [1] -39.05 -39.00 -38.95 -38.90 -38.85 -38.80
Next, we can read the time variable, which represents each day in 2019.
#Reading the time variable
t <- ncvar_get(ncin, "time")
tunits <- ncatt_get(ncin, "time", "units")
nt <- dim(t)
head(t)
## [1] 0 1 2 3 4 5
Now we can read the whole array which contains the latitude, longitude and time values.
#get the variable and its attributes, filtering the longitude and lagitude values
daily_rain_array <- ncvar_get( ncin, dname, start=c(min(lonIdx), min(latIdx),min(t)+1), count=c((max(lonIdx)-min(lonIdx)+1),(max(latIdx)-min(latIdx)+1),(max(t)-min(t)+1)))
dlname <- ncatt_get(ncin, dname, "long_name")
dunits <- ncatt_get(ncin, dname, "units")
fillvalue <- ncatt_get(ncin, dname, "_FillValue")
dim(daily_rain_array)
## [1] 177 101 283
#Close nc file
nc_close(ncin)
# Convert the time units string into a readable format
tustr <- strsplit(tunits$value, " ")
tdstr <- strsplit(unlist(tustr)[3], "-")
tmonth = as.integer(unlist(tdstr)[2])
tday = as.integer(unlist(tdstr)[3])
tyear = as.integer(unlist(tdstr)[1])
chron(t, origin = c(tmonth, tday, tyear))
## [1] 01/01/19 01/02/19 01/03/19 01/04/19 01/05/19 01/06/19 01/07/19
## [8] 01/08/19 01/09/19 01/10/19 01/11/19 01/12/19 01/13/19 01/14/19
## [15] 01/15/19 01/16/19 01/17/19 01/18/19 01/19/19 01/20/19 01/21/19
## [22] 01/22/19 01/23/19 01/24/19 01/25/19 01/26/19 01/27/19 01/28/19
## [29] 01/29/19 01/30/19 01/31/19 02/01/19 02/02/19 02/03/19 02/04/19
## [36] 02/05/19 02/06/19 02/07/19 02/08/19 02/09/19 02/10/19 02/11/19
## [43] 02/12/19 02/13/19 02/14/19 02/15/19 02/16/19 02/17/19 02/18/19
## [50] 02/19/19 02/20/19 02/21/19 02/22/19 02/23/19 02/24/19 02/25/19
## [57] 02/26/19 02/27/19 02/28/19 03/01/19 03/02/19 03/03/19 03/04/19
## [64] 03/05/19 03/06/19 03/07/19 03/08/19 03/09/19 03/10/19 03/11/19
## [71] 03/12/19 03/13/19 03/14/19 03/15/19 03/16/19 03/17/19 03/18/19
## [78] 03/19/19 03/20/19 03/21/19 03/22/19 03/23/19 03/24/19 03/25/19
## [85] 03/26/19 03/27/19 03/28/19 03/29/19 03/30/19 03/31/19 04/01/19
## [92] 04/02/19 04/03/19 04/04/19 04/05/19 04/06/19 04/07/19 04/08/19
## [99] 04/09/19 04/10/19 04/11/19 04/12/19 04/13/19 04/14/19 04/15/19
## [106] 04/16/19 04/17/19 04/18/19 04/19/19 04/20/19 04/21/19 04/22/19
## [113] 04/23/19 04/24/19 04/25/19 04/26/19 04/27/19 04/28/19 04/29/19
## [120] 04/30/19 05/01/19 05/02/19 05/03/19 05/04/19 05/05/19 05/06/19
## [127] 05/07/19 05/08/19 05/09/19 05/10/19 05/11/19 05/12/19 05/13/19
## [134] 05/14/19 05/15/19 05/16/19 05/17/19 05/18/19 05/19/19 05/20/19
## [141] 05/21/19 05/22/19 05/23/19 05/24/19 05/25/19 05/26/19 05/27/19
## [148] 05/28/19 05/29/19 05/30/19 05/31/19 06/01/19 06/02/19 06/03/19
## [155] 06/04/19 06/05/19 06/06/19 06/07/19 06/08/19 06/09/19 06/10/19
## [162] 06/11/19 06/12/19 06/13/19 06/14/19 06/15/19 06/16/19 06/17/19
## [169] 06/18/19 06/19/19 06/20/19 06/21/19 06/22/19 06/23/19 06/24/19
## [176] 06/25/19 06/26/19 06/27/19 06/28/19 06/29/19 06/30/19 07/01/19
## [183] 07/02/19 07/03/19 07/04/19 07/05/19 07/06/19 07/07/19 07/08/19
## [190] 07/09/19 07/10/19 07/11/19 07/12/19 07/13/19 07/14/19 07/15/19
## [197] 07/16/19 07/17/19 07/18/19 07/19/19 07/20/19 07/21/19 07/22/19
## [204] 07/23/19 07/24/19 07/25/19 07/26/19 07/27/19 07/28/19 07/29/19
## [211] 07/30/19 07/31/19 08/01/19 08/02/19 08/03/19 08/04/19 08/05/19
## [218] 08/06/19 08/07/19 08/08/19 08/09/19 08/10/19 08/11/19 08/12/19
## [225] 08/13/19 08/14/19 08/15/19 08/16/19 08/17/19 08/18/19 08/19/19
## [232] 08/20/19 08/21/19 08/22/19 08/23/19 08/24/19 08/25/19 08/26/19
## [239] 08/27/19 08/28/19 08/29/19 08/30/19 08/31/19 09/01/19 09/02/19
## [246] 09/03/19 09/04/19 09/05/19 09/06/19 09/07/19 09/08/19 09/09/19
## [253] 09/10/19 09/11/19 09/12/19 09/13/19 09/14/19 09/15/19 09/16/19
## [260] 09/17/19 09/18/19 09/19/19 09/20/19 09/21/19 09/22/19 09/23/19
## [267] 09/24/19 09/25/19 09/26/19 09/27/19 09/28/19 09/29/19 09/30/19
## [274] 10/01/19 10/02/19 10/03/19 10/04/19 10/05/19 10/06/19 10/07/19
## [281] 10/08/19 10/09/19 10/10/19
Replace the NetCDF missing values to NA
# Replace NetCDF missing values to NA
daily_rain_array[daily_rain_array == fillvalue$value] <- NA
Converting the array to a matrix
#Convert the nlon by nlat by nt array into a nlon by nlat by nt matrix
daily_rain_vec._long <- as.vector(daily_rain_array)
length(daily_rain_vec._long)
## [1] 5059191
# Reshape vector into a matrix using the matrix() function
daily_rain_mat <- matrix(daily_rain_vec._long, nrow = nlon * nlat, ncol = nt)
dim(daily_rain_mat)
## [1] 17877 283
head(na.omit(daily_rain_mat,20))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] 0 0 0 0 0.0 0.1 0 0 0.0 0 0 0 0
## [2,] 0 0 0 0 0.1 0.1 0 0 0.1 0 0 0 0
## [3,] 0 0 0 0 0.1 0.3 0 0 0.3 0 0 0 0
## [4,] 0 0 0 0 0.2 0.4 0 0 0.5 0 0 0 0
## [5,] 0 0 0 0 0.1 0.2 0 0 0.1 0 0 0 0
## [6,] 0 0 0 0 0.0 0.2 0 0 0.2 0 0 0 0
## [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
## [1,] 0 0 0 0.6 0.7 0.1 0.0 0 0 0 0
## [2,] 0 0 0 0.6 1.0 0.3 0.0 0 0 0 0
## [3,] 0 0 0 0.5 1.4 0.8 0.1 0 0 0 0
## [4,] 0 0 0 0.2 1.4 0.9 0.1 0 0 0 0
## [5,] 0 0 0 0.4 0.6 0.0 0.0 0 0 0 0
## [6,] 0 0 0 0.6 0.6 0.0 0.0 0 0 0 0
## [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35]
## [1,] 0 0.0 0 0.0 0 1.4 0.2 0 0 0 0.0
## [2,] 0 0.0 0 0.1 0 1.3 0.3 0 0 0 0.0
## [3,] 0 0.0 0 0.2 0 0.9 1.4 0 0 0 0.0
## [4,] 0 0.1 0 0.1 0 0.5 2.2 0 0 0 0.0
## [5,] 0 0.0 0 0.0 0 1.0 2.5 0 0 0 0.1
## [6,] 0 0.0 0 0.0 0 1.4 2.0 0 0 0 0.0
## [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46]
## [1,] 13.7 2.7 0.2 0 9.6 1.5 0.0 18.1 5.7 0.2 0.0
## [2,] 13.4 3.7 0.7 0 8.4 3.4 0.1 17.8 12.4 0.4 0.0
## [3,] 8.9 4.9 1.5 0 5.1 7.0 0.5 11.8 24.5 1.0 0.1
## [4,] 4.9 5.4 1.9 0 2.5 8.6 0.6 6.4 29.5 1.2 0.2
## [5,] 11.1 2.2 0.1 0 9.3 1.6 0.1 14.6 5.5 0.0 0.0
## [6,] 14.5 3.1 0.1 0 9.9 0.9 0.3 19.3 4.2 0.0 0.0
## [,47] [,48] [,49] [,50] [,51] [,52] [,53] [,54] [,55] [,56] [,57]
## [1,] 0 0 0 0 0 0.9 0.0 0.7 0 0 0
## [2,] 0 0 0 0 0 1.2 0.0 0.0 0 0 0
## [3,] 0 0 0 0 0 1.4 0.0 0.0 0 0 0
## [4,] 0 0 0 0 0 1.4 0.0 0.0 0 0 0
## [5,] 0 0 0 0 0 0.7 0.1 1.6 0 0 0
## [6,] 0 0 0 0 0 0.9 0.0 0.5 0 0 0
## [,58] [,59] [,60] [,61] [,62] [,63] [,64] [,65] [,66] [,67] [,68]
## [1,] 0 0.0 0.3 0 0 0 4.2 1.1 0.0 0.2 0.4
## [2,] 0 0.1 0.7 0 0 0 4.0 1.9 0.1 0.2 0.0
## [3,] 0 0.3 1.5 0 0 0 2.7 3.3 0.6 0.1 0.0
## [4,] 0 0.6 1.7 0 0 0 1.5 3.7 0.8 0.1 0.0
## [5,] 0 0.0 0.0 0 0 0 3.4 1.0 0.2 0.1 1.0
## [6,] 0 0.1 0.0 0 0 0 4.5 1.3 0.2 0.2 0.3
## [,69] [,70] [,71] [,72] [,73] [,74] [,75] [,76] [,77] [,78] [,79]
## [1,] 0 4.5 0.5 10.6 0 0 0.1 0.1 1.6 0.7 2.0
## [2,] 0 5.5 1.5 10.5 0 0 0.1 0.1 1.8 1.0 2.1
## [3,] 0 6.8 3.3 8.1 0 0 0.0 0.0 1.4 1.2 1.7
## [4,] 0 6.3 3.9 5.4 0 0 0.0 0.0 1.0 1.3 1.4
## [5,] 0 3.8 0.2 8.9 0 0 0.1 0.0 1.4 0.6 1.7
## [6,] 0 4.2 0.2 11.5 0 0 0.1 0.1 1.9 1.2 2.2
## [,80] [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90]
## [1,] 0.7 0.3 0.4 7.9 8.2 7.6 0.1 0 1.7 2.0 0.7
## [2,] 0.8 0.3 0.4 7.7 9.3 9.8 0.3 0 1.6 3.0 0.8
## [3,] 1.0 0.6 0.3 5.3 9.7 13.0 0.2 0 1.1 4.3 1.1
## [4,] 1.0 0.7 0.2 2.9 8.5 13.0 0.2 0 0.6 4.4 1.1
## [5,] 0.8 0.7 0.4 6.5 6.8 6.4 0.0 0 1.4 2.1 1.6
## [6,] 1.0 0.4 0.4 8.5 8.6 7.1 0.0 0 1.8 2.4 1.6
## [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100] [,101]
## [1,] 0.2 0 0 0 0.2 0.0 0.5 6.8 2.3 0.0 0.0
## [2,] 0.4 0 0 0 0.2 0.2 0.5 6.4 4.0 0.5 0.0
## [3,] 0.7 0 0 0 0.1 0.5 0.3 5.0 8.1 1.4 0.1
## [4,] 0.9 0 0 0 0.1 0.5 0.2 3.2 9.4 2.0 0.2
## [5,] 0.2 0 0 0 0.1 0.0 0.4 6.0 2.7 0.1 0.1
## [6,] 0.0 0 0 0 0.2 0.0 0.5 7.6 1.9 0.4 0.0
## [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109] [,110] [,111]
## [1,] 0.2 4.0 2.2 0 0 0 0.2 0 0 3.5
## [2,] 0.2 1.9 2.1 0 0 0 0.2 0 0 3.2
## [3,] 0.2 0.6 2.0 0 0 0 0.1 0 0 2.4
## [4,] 0.2 0.0 1.6 0 0 0 0.1 0 0 1.5
## [5,] 0.1 6.2 1.7 0 0 0 0.2 0 0 3.0
## [6,] 0.2 3.6 2.1 0 0 0 0.2 0 0 3.6
## [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120] [,121]
## [1,] 2.1 1.7 2.1 1.0 2.5 3.6 3.4 0.9 0 5.0
## [2,] 2.5 1.5 2.5 1.2 3.0 3.6 3.5 1.3 0 4.8
## [3,] 3.4 1.0 2.9 1.5 3.9 3.4 3.9 2.3 0 3.3
## [4,] 3.4 0.5 2.9 1.5 4.1 2.7 3.5 2.5 0 1.7
## [5,] 2.0 1.3 1.9 0.8 3.1 3.3 3.8 1.1 0 4.1
## [6,] 2.1 1.8 2.6 1.0 3.3 3.8 3.8 1.1 0 5.3
## [,122] [,123] [,124] [,125] [,126] [,127] [,128] [,129] [,130] [,131]
## [1,] 6.0 13.0 3.4 0.1 0.0 4.8 13.6 0.7 29.1 6.0
## [2,] 7.1 12.9 4.3 0.0 0.1 4.6 14.7 1.7 27.8 4.9
## [3,] 8.2 11.3 8.9 0.0 0.3 3.2 15.3 3.9 19.0 3.8
## [4,] 7.3 8.6 10.4 0.0 0.4 1.6 12.8 4.6 9.9 2.5
## [5,] 5.2 12.0 5.2 0.4 0.1 4.0 12.8 0.7 24.6 8.8
## [6,] 6.3 14.2 1.8 0.2 0.2 5.3 14.3 0.4 31.2 7.7
## [,132] [,133] [,134] [,135] [,136] [,137] [,138] [,139] [,140] [,141]
## [1,] 2.1 0.2 0.2 1.1 0.0 0 0 0.0 5.5 0.2
## [2,] 2.1 0.5 0.2 1.4 0.0 0 0 0.0 6.0 0.1
## [3,] 2.3 1.1 0.1 1.8 0.1 0 0 0.0 5.8 0.5
## [4,] 1.9 1.3 0.1 1.8 0.1 0 0 0.0 4.6 0.6
## [5,] 2.5 0.5 0.1 0.9 0.0 0 0 0.1 4.4 1.0
## [6,] 2.3 0.3 0.2 0.9 0.0 0 0 0.0 5.5 0.5
## [,142] [,143] [,144] [,145] [,146] [,147] [,148] [,149] [,150] [,151]
## [1,] 0 4.0 4.9 18.3 29.0 18.1 10.1 32.6 2.8 2.6
## [2,] 0 4.3 5.2 19.8 30.8 21.0 11.4 36.4 2.9 2.8
## [3,] 0 4.2 5.4 19.4 30.3 25.7 13.3 42.7 3.8 3.5
## [4,] 0 3.2 4.5 15.5 24.4 24.0 12.4 38.9 4.2 3.2
## [5,] 0 3.3 4.3 15.5 25.9 19.2 10.3 34.7 4.3 2.8
## [6,] 0 3.9 5.0 18.4 29.7 19.8 11.4 35.1 4.3 2.1
## [,152] [,153] [,154] [,155] [,156] [,157] [,158] [,159] [,160] [,161]
## [1,] 0 0.8 20.0 2.6 3.1 1.3 4.1 0.0 4.9 2.9
## [2,] 0 0.9 18.0 1.4 3.1 1.3 3.5 0.0 4.5 3.2
## [3,] 0 0.6 11.4 1.6 2.5 1.3 2.3 0.0 2.9 3.8
## [4,] 0 0.3 5.7 1.7 2.0 1.0 1.1 0.0 1.5 3.6
## [5,] 0 0.5 17.2 7.1 2.8 1.4 3.7 0.1 4.1 2.9
## [6,] 0 0.9 21.7 4.3 4.1 1.2 4.5 0.0 5.4 2.9
## [,162] [,163] [,164] [,165] [,166] [,167] [,168] [,169] [,170] [,171]
## [1,] 0.5 15.5 9.5 10.6 2.5 0.6 0.1 0 1.6 1.5
## [2,] 0.9 14.1 11.7 11.4 2.5 0.7 0.2 0 2.1 0.8
## [3,] 2.0 9.1 16.5 12.0 3.2 1.2 0.4 0 3.8 0.5
## [4,] 2.2 4.7 16.6 10.6 3.2 1.2 0.3 0 4.3 0.3
## [5,] 0.7 13.0 11.2 10.1 4.0 0.6 0.1 0 3.5 2.8
## [6,] 0.4 16.9 8.9 12.0 2.9 0.4 0.4 0 2.2 2.2
## [,172] [,173] [,174] [,175] [,176] [,177] [,178] [,179] [,180] [,181]
## [1,] 0 0 0.1 0 0.2 0 0 0.0 18.9 3.1
## [2,] 0 0 0.1 0 0.2 0 0 0.0 17.2 7.1
## [3,] 0 0 0.0 0 0.1 0 0 0.0 11.0 14.7
## [4,] 0 0 0.0 0 0.0 0 0 0.0 5.6 16.5
## [5,] 0 0 0.1 0 0.2 0 0 0.2 16.1 3.2
## [6,] 0 0 0.1 0 0.3 0 0 0.0 20.8 1.4
## [,182] [,183] [,184] [,185] [,186] [,187] [,188] [,189] [,190] [,191]
## [1,] 9.2 0.1 1.1 0.0 1.4 0 0.3 1.3 4.8 5.3
## [2,] 11.6 0.1 1.2 0.0 1.6 0 0.3 1.7 5.9 6.7
## [3,] 14.1 0.1 1.2 0.0 1.8 0 0.4 2.2 6.8 8.1
## [4,] 12.7 0.1 1.0 0.0 1.5 0 0.4 1.9 5.9 7.3
## [5,] 7.2 0.0 1.3 0.2 1.4 0 0.3 1.1 4.2 4.2
## [6,] 9.1 0.1 1.2 0.0 1.7 0 0.3 1.4 4.8 5.4
## [,192] [,193] [,194] [,195] [,196] [,197] [,198] [,199] [,200] [,201]
## [1,] 14.4 19.3 18.7 8.7 14.9 1.0 4.2 6.6 2.6 0
## [2,] 17.0 23.0 20.8 10.1 17.5 1.1 5.1 7.9 2.9 0
## [3,] 19.0 26.2 22.4 11.3 19.9 1.0 5.9 9.2 3.4 0
## [4,] 16.8 23.0 19.2 10.1 17.8 0.8 5.3 8.2 3.0 0
## [5,] 14.3 18.5 20.0 8.9 15.0 1.0 3.7 6.0 2.3 0
## [6,] 16.3 21.5 21.6 10.0 17.6 0.9 4.4 7.0 2.7 0
## [,202] [,203] [,204] [,205] [,206] [,207] [,208] [,209] [,210] [,211]
## [1,] 0 0.2 0 9.9 0.9 0 0.2 0 0.2 1.3
## [2,] 0 0.4 0 12.1 1.1 0 0.2 0 0.2 1.5
## [3,] 0 0.5 0 13.7 1.3 0 0.1 0 0.2 1.7
## [4,] 0 0.4 0 12.2 1.2 0 0.1 0 0.2 1.4
## [5,] 0 0.1 0 10.4 0.6 0 0.4 0 0.1 1.4
## [6,] 0 0.2 0 12.4 0.8 0 0.3 0 0.2 1.7
## [,212] [,213] [,214] [,215] [,216] [,217] [,218] [,219] [,220] [,221]
## [1,] 0.1 0.1 0.3 0.6 2.1 0.1 0.3 0.8 9.4 7.2
## [2,] 0.2 0.2 0.3 0.7 2.5 0.1 0.2 0.9 11.3 9.3
## [3,] 0.2 0.3 0.4 0.8 2.7 0.1 0.3 1.3 13.4 11.4
## [4,] 0.2 0.2 0.2 0.8 2.5 0.2 0.2 1.1 12.5 10.6
## [5,] 0.1 0.1 0.4 0.5 2.0 0.1 0.4 0.4 9.5 6.0
## [6,] 0.2 0.1 0.4 0.6 2.2 0.1 0.4 0.6 10.7 7.3
## [,222] [,223] [,224] [,225] [,226] [,227] [,228] [,229] [,230] [,231]
## [1,] 7.0 3.9 2.0 1.7 1.2 0 8.2 3.6 0.1 9.6
## [2,] 8.2 4.5 2.9 2.1 1.5 0 5.2 4.5 0.0 11.9
## [3,] 9.6 5.2 3.5 2.5 1.9 0 2.3 5.6 0.0 14.3
## [4,] 8.7 4.8 3.7 2.3 1.7 0 0.0 5.3 0.0 13.5
## [5,] 7.0 4.4 2.3 1.4 1.0 0 11.6 3.0 0.1 9.6
## [6,] 7.6 4.4 3.8 1.6 1.2 0 7.9 3.6 0.1 11.1
## [,232] [,233] [,234] [,235] [,236] [,237] [,238] [,239] [,240] [,241]
## [1,] 4.2 13.8 3.5 0 0 3.8 1.2 2.3 1.0 1.8
## [2,] 5.1 15.8 3.6 0 0 4.6 1.6 2.9 1.3 1.9
## [3,] 5.9 18.2 3.7 0 0 5.4 2.0 3.4 1.8 2.1
## [4,] 5.3 16.4 3.2 0 0 4.9 1.9 3.1 1.7 1.8
## [5,] 3.9 14.6 4.6 0 0 3.4 1.3 2.0 0.6 2.2
## [6,] 4.3 15.9 4.7 0 0 3.8 1.7 2.3 0.9 2.2
## [,242] [,243] [,244] [,245] [,246] [,247] [,248] [,249] [,250] [,251]
## [1,] 0.3 7.4 0 7.5 0.7 0.0 0.0 4.1 9.9 4.2
## [2,] 0.4 4.7 0 8.0 0.6 0.1 0.0 4.8 10.5 3.9
## [3,] 0.5 2.0 0 8.9 0.6 0.2 0.0 5.8 11.6 3.7
## [4,] 0.5 0.0 0 8.2 0.3 0.1 0.0 6.1 11.2 3.2
## [5,] 0.3 10.6 0 7.1 0.9 0.0 0.1 4.2 9.7 5.4
## [6,] 0.6 7.1 0 7.6 0.8 0.1 0.0 5.1 9.9 4.8
## [,252] [,253] [,254] [,255] [,256] [,257] [,258] [,259] [,260] [,261]
## [1,] 4.9 0 0 0.2 0 0.1 1.8 13.6 0 0.1
## [2,] 5.6 0 0 0.2 0 0.1 2.0 14.3 0 0.1
## [3,] 6.6 0 0 0.2 0 0.0 2.1 15.4 0 0.0
## [4,] 6.3 0 0 0.2 0 0.0 2.0 13.8 0 0.0
## [5,] 5.2 0 0 0.1 0 0.1 1.7 14.0 0 0.2
## [6,] 6.5 0 0 0.1 0 0.1 1.8 15.1 0 0.1
## [,262] [,263] [,264] [,265] [,266] [,267] [,268] [,269] [,270] [,271]
## [1,] 0.1 0 0 6.6 4.6 4.2 0.5 0.6 0 6.3
## [2,] 0.0 0 0 6.5 5.1 4.4 0.3 0.8 0 6.5
## [3,] 0.0 0 0 6.8 5.8 4.6 0.1 0.8 0 6.9
## [4,] 0.0 0 0 6.1 5.4 4.1 0.0 0.8 0 6.2
## [5,] 0.1 0 0 7.5 3.8 4.3 0.9 0.8 0 6.6
## [6,] 0.1 0 0 7.5 4.4 4.2 0.7 1.1 0 6.9
## [,272] [,273] [,274] [,275] [,276] [,277] [,278] [,279] [,280] [,281]
## [1,] 0.2 0.1 0 0 0 0.0 0 0.1 0.0 3.6
## [2,] 0.0 0.1 0 0 0 0.1 0 0.0 0.0 3.1
## [3,] 0.0 0.1 0 0 0 0.0 0 0.0 0.1 2.6
## [4,] 0.0 0.1 0 0 0 0.0 0 0.0 0.2 2.2
## [5,] 0.1 0.1 0 0 0 0.0 0 0.1 0.0 4.4
## [6,] 0.1 0.1 0 0 0 0.0 0 0.1 0.0 3.9
## [,282] [,283]
## [1,] 0 0.2
## [2,] 0 0.2
## [3,] 0 0.1
## [4,] 0 0.1
## [5,] 0 0.4
## [6,] 0 0.3
Creating data frame from the daily_rain_mat matrix.
# The expand.grid() function is used to create the pairs of values of longitude and latitude
lonlat <- expand.grid(lon, lat)
daily_rain_df <- data.frame(cbind(lonlat, daily_rain_mat))
options(width = 110)
#Rename Var1 and Var2 columns to Long and Lat and the remaining columns to Date
daily_rain_df <- daily_rain_df %>% dplyr::rename (Lat = Var2)
daily_rain_df <- daily_rain_df %>% dplyr::rename (Long = Var1)
names(daily_rain_df)[3:(max(t)+3)] <- format(seq(as.Date("2019/01/01"), by = "day", length.out = (max(t)+1)))
head(na.omit(daily_rain_df, 20))
## Long Lat 2019-01-01 2019-01-02 2019-01-03 2019-01-04 2019-01-05 2019-01-06 2019-01-07 2019-01-08
## 108 146.30 -39.05 0 0 0 0 0.0 0.1 0 0
## 109 146.35 -39.05 0 0 0 0 0.1 0.1 0 0
## 110 146.40 -39.05 0 0 0 0 0.1 0.3 0 0
## 111 146.45 -39.05 0 0 0 0 0.2 0.4 0 0
## 284 146.25 -39.00 0 0 0 0 0.1 0.2 0 0
## 285 146.30 -39.00 0 0 0 0 0.0 0.2 0 0
## 2019-01-09 2019-01-10 2019-01-11 2019-01-12 2019-01-13 2019-01-14 2019-01-15 2019-01-16 2019-01-17
## 108 0.0 0 0 0 0 0 0 0 0.6
## 109 0.1 0 0 0 0 0 0 0 0.6
## 110 0.3 0 0 0 0 0 0 0 0.5
## 111 0.5 0 0 0 0 0 0 0 0.2
## 284 0.1 0 0 0 0 0 0 0 0.4
## 285 0.2 0 0 0 0 0 0 0 0.6
## 2019-01-18 2019-01-19 2019-01-20 2019-01-21 2019-01-22 2019-01-23 2019-01-24 2019-01-25 2019-01-26
## 108 0.7 0.1 0.0 0 0 0 0 0 0.0
## 109 1.0 0.3 0.0 0 0 0 0 0 0.0
## 110 1.4 0.8 0.1 0 0 0 0 0 0.0
## 111 1.4 0.9 0.1 0 0 0 0 0 0.1
## 284 0.6 0.0 0.0 0 0 0 0 0 0.0
## 285 0.6 0.0 0.0 0 0 0 0 0 0.0
## 2019-01-27 2019-01-28 2019-01-29 2019-01-30 2019-01-31 2019-02-01 2019-02-02 2019-02-03 2019-02-04
## 108 0 0.0 0 1.4 0.2 0 0 0 0.0
## 109 0 0.1 0 1.3 0.3 0 0 0 0.0
## 110 0 0.2 0 0.9 1.4 0 0 0 0.0
## 111 0 0.1 0 0.5 2.2 0 0 0 0.0
## 284 0 0.0 0 1.0 2.5 0 0 0 0.1
## 285 0 0.0 0 1.4 2.0 0 0 0 0.0
## 2019-02-05 2019-02-06 2019-02-07 2019-02-08 2019-02-09 2019-02-10 2019-02-11 2019-02-12 2019-02-13
## 108 13.7 2.7 0.2 0 9.6 1.5 0.0 18.1 5.7
## 109 13.4 3.7 0.7 0 8.4 3.4 0.1 17.8 12.4
## 110 8.9 4.9 1.5 0 5.1 7.0 0.5 11.8 24.5
## 111 4.9 5.4 1.9 0 2.5 8.6 0.6 6.4 29.5
## 284 11.1 2.2 0.1 0 9.3 1.6 0.1 14.6 5.5
## 285 14.5 3.1 0.1 0 9.9 0.9 0.3 19.3 4.2
## 2019-02-14 2019-02-15 2019-02-16 2019-02-17 2019-02-18 2019-02-19 2019-02-20 2019-02-21 2019-02-22
## 108 0.2 0.0 0 0 0 0 0 0.9 0.0
## 109 0.4 0.0 0 0 0 0 0 1.2 0.0
## 110 1.0 0.1 0 0 0 0 0 1.4 0.0
## 111 1.2 0.2 0 0 0 0 0 1.4 0.0
## 284 0.0 0.0 0 0 0 0 0 0.7 0.1
## 285 0.0 0.0 0 0 0 0 0 0.9 0.0
## 2019-02-23 2019-02-24 2019-02-25 2019-02-26 2019-02-27 2019-02-28 2019-03-01 2019-03-02 2019-03-03
## 108 0.7 0 0 0 0 0.0 0.3 0 0
## 109 0.0 0 0 0 0 0.1 0.7 0 0
## 110 0.0 0 0 0 0 0.3 1.5 0 0
## 111 0.0 0 0 0 0 0.6 1.7 0 0
## 284 1.6 0 0 0 0 0.0 0.0 0 0
## 285 0.5 0 0 0 0 0.1 0.0 0 0
## 2019-03-04 2019-03-05 2019-03-06 2019-03-07 2019-03-08 2019-03-09 2019-03-10 2019-03-11 2019-03-12
## 108 0 4.2 1.1 0.0 0.2 0.4 0 4.5 0.5
## 109 0 4.0 1.9 0.1 0.2 0.0 0 5.5 1.5
## 110 0 2.7 3.3 0.6 0.1 0.0 0 6.8 3.3
## 111 0 1.5 3.7 0.8 0.1 0.0 0 6.3 3.9
## 284 0 3.4 1.0 0.2 0.1 1.0 0 3.8 0.2
## 285 0 4.5 1.3 0.2 0.2 0.3 0 4.2 0.2
## 2019-03-13 2019-03-14 2019-03-15 2019-03-16 2019-03-17 2019-03-18 2019-03-19 2019-03-20 2019-03-21
## 108 10.6 0 0 0.1 0.1 1.6 0.7 2.0 0.7
## 109 10.5 0 0 0.1 0.1 1.8 1.0 2.1 0.8
## 110 8.1 0 0 0.0 0.0 1.4 1.2 1.7 1.0
## 111 5.4 0 0 0.0 0.0 1.0 1.3 1.4 1.0
## 284 8.9 0 0 0.1 0.0 1.4 0.6 1.7 0.8
## 285 11.5 0 0 0.1 0.1 1.9 1.2 2.2 1.0
## 2019-03-22 2019-03-23 2019-03-24 2019-03-25 2019-03-26 2019-03-27 2019-03-28 2019-03-29 2019-03-30
## 108 0.3 0.4 7.9 8.2 7.6 0.1 0 1.7 2.0
## 109 0.3 0.4 7.7 9.3 9.8 0.3 0 1.6 3.0
## 110 0.6 0.3 5.3 9.7 13.0 0.2 0 1.1 4.3
## 111 0.7 0.2 2.9 8.5 13.0 0.2 0 0.6 4.4
## 284 0.7 0.4 6.5 6.8 6.4 0.0 0 1.4 2.1
## 285 0.4 0.4 8.5 8.6 7.1 0.0 0 1.8 2.4
## 2019-03-31 2019-04-01 2019-04-02 2019-04-03 2019-04-04 2019-04-05 2019-04-06 2019-04-07 2019-04-08
## 108 0.7 0.2 0 0 0 0.2 0.0 0.5 6.8
## 109 0.8 0.4 0 0 0 0.2 0.2 0.5 6.4
## 110 1.1 0.7 0 0 0 0.1 0.5 0.3 5.0
## 111 1.1 0.9 0 0 0 0.1 0.5 0.2 3.2
## 284 1.6 0.2 0 0 0 0.1 0.0 0.4 6.0
## 285 1.6 0.0 0 0 0 0.2 0.0 0.5 7.6
## 2019-04-09 2019-04-10 2019-04-11 2019-04-12 2019-04-13 2019-04-14 2019-04-15 2019-04-16 2019-04-17
## 108 2.3 0.0 0.0 0.2 4.0 2.2 0 0 0
## 109 4.0 0.5 0.0 0.2 1.9 2.1 0 0 0
## 110 8.1 1.4 0.1 0.2 0.6 2.0 0 0 0
## 111 9.4 2.0 0.2 0.2 0.0 1.6 0 0 0
## 284 2.7 0.1 0.1 0.1 6.2 1.7 0 0 0
## 285 1.9 0.4 0.0 0.2 3.6 2.1 0 0 0
## 2019-04-18 2019-04-19 2019-04-20 2019-04-21 2019-04-22 2019-04-23 2019-04-24 2019-04-25 2019-04-26
## 108 0.2 0 0 3.5 2.1 1.7 2.1 1.0 2.5
## 109 0.2 0 0 3.2 2.5 1.5 2.5 1.2 3.0
## 110 0.1 0 0 2.4 3.4 1.0 2.9 1.5 3.9
## 111 0.1 0 0 1.5 3.4 0.5 2.9 1.5 4.1
## 284 0.2 0 0 3.0 2.0 1.3 1.9 0.8 3.1
## 285 0.2 0 0 3.6 2.1 1.8 2.6 1.0 3.3
## 2019-04-27 2019-04-28 2019-04-29 2019-04-30 2019-05-01 2019-05-02 2019-05-03 2019-05-04 2019-05-05
## 108 3.6 3.4 0.9 0 5.0 6.0 13.0 3.4 0.1
## 109 3.6 3.5 1.3 0 4.8 7.1 12.9 4.3 0.0
## 110 3.4 3.9 2.3 0 3.3 8.2 11.3 8.9 0.0
## 111 2.7 3.5 2.5 0 1.7 7.3 8.6 10.4 0.0
## 284 3.3 3.8 1.1 0 4.1 5.2 12.0 5.2 0.4
## 285 3.8 3.8 1.1 0 5.3 6.3 14.2 1.8 0.2
## 2019-05-06 2019-05-07 2019-05-08 2019-05-09 2019-05-10 2019-05-11 2019-05-12 2019-05-13 2019-05-14
## 108 0.0 4.8 13.6 0.7 29.1 6.0 2.1 0.2 0.2
## 109 0.1 4.6 14.7 1.7 27.8 4.9 2.1 0.5 0.2
## 110 0.3 3.2 15.3 3.9 19.0 3.8 2.3 1.1 0.1
## 111 0.4 1.6 12.8 4.6 9.9 2.5 1.9 1.3 0.1
## 284 0.1 4.0 12.8 0.7 24.6 8.8 2.5 0.5 0.1
## 285 0.2 5.3 14.3 0.4 31.2 7.7 2.3 0.3 0.2
## 2019-05-15 2019-05-16 2019-05-17 2019-05-18 2019-05-19 2019-05-20 2019-05-21 2019-05-22 2019-05-23
## 108 1.1 0.0 0 0 0.0 5.5 0.2 0 4.0
## 109 1.4 0.0 0 0 0.0 6.0 0.1 0 4.3
## 110 1.8 0.1 0 0 0.0 5.8 0.5 0 4.2
## 111 1.8 0.1 0 0 0.0 4.6 0.6 0 3.2
## 284 0.9 0.0 0 0 0.1 4.4 1.0 0 3.3
## 285 0.9 0.0 0 0 0.0 5.5 0.5 0 3.9
## 2019-05-24 2019-05-25 2019-05-26 2019-05-27 2019-05-28 2019-05-29 2019-05-30 2019-05-31 2019-06-01
## 108 4.9 18.3 29.0 18.1 10.1 32.6 2.8 2.6 0
## 109 5.2 19.8 30.8 21.0 11.4 36.4 2.9 2.8 0
## 110 5.4 19.4 30.3 25.7 13.3 42.7 3.8 3.5 0
## 111 4.5 15.5 24.4 24.0 12.4 38.9 4.2 3.2 0
## 284 4.3 15.5 25.9 19.2 10.3 34.7 4.3 2.8 0
## 285 5.0 18.4 29.7 19.8 11.4 35.1 4.3 2.1 0
## 2019-06-02 2019-06-03 2019-06-04 2019-06-05 2019-06-06 2019-06-07 2019-06-08 2019-06-09 2019-06-10
## 108 0.8 20.0 2.6 3.1 1.3 4.1 0.0 4.9 2.9
## 109 0.9 18.0 1.4 3.1 1.3 3.5 0.0 4.5 3.2
## 110 0.6 11.4 1.6 2.5 1.3 2.3 0.0 2.9 3.8
## 111 0.3 5.7 1.7 2.0 1.0 1.1 0.0 1.5 3.6
## 284 0.5 17.2 7.1 2.8 1.4 3.7 0.1 4.1 2.9
## 285 0.9 21.7 4.3 4.1 1.2 4.5 0.0 5.4 2.9
## 2019-06-11 2019-06-12 2019-06-13 2019-06-14 2019-06-15 2019-06-16 2019-06-17 2019-06-18 2019-06-19
## 108 0.5 15.5 9.5 10.6 2.5 0.6 0.1 0 1.6
## 109 0.9 14.1 11.7 11.4 2.5 0.7 0.2 0 2.1
## 110 2.0 9.1 16.5 12.0 3.2 1.2 0.4 0 3.8
## 111 2.2 4.7 16.6 10.6 3.2 1.2 0.3 0 4.3
## 284 0.7 13.0 11.2 10.1 4.0 0.6 0.1 0 3.5
## 285 0.4 16.9 8.9 12.0 2.9 0.4 0.4 0 2.2
## 2019-06-20 2019-06-21 2019-06-22 2019-06-23 2019-06-24 2019-06-25 2019-06-26 2019-06-27 2019-06-28
## 108 1.5 0 0 0.1 0 0.2 0 0 0.0
## 109 0.8 0 0 0.1 0 0.2 0 0 0.0
## 110 0.5 0 0 0.0 0 0.1 0 0 0.0
## 111 0.3 0 0 0.0 0 0.0 0 0 0.0
## 284 2.8 0 0 0.1 0 0.2 0 0 0.2
## 285 2.2 0 0 0.1 0 0.3 0 0 0.0
## 2019-06-29 2019-06-30 2019-07-01 2019-07-02 2019-07-03 2019-07-04 2019-07-05 2019-07-06 2019-07-07
## 108 18.9 3.1 9.2 0.1 1.1 0.0 1.4 0 0.3
## 109 17.2 7.1 11.6 0.1 1.2 0.0 1.6 0 0.3
## 110 11.0 14.7 14.1 0.1 1.2 0.0 1.8 0 0.4
## 111 5.6 16.5 12.7 0.1 1.0 0.0 1.5 0 0.4
## 284 16.1 3.2 7.2 0.0 1.3 0.2 1.4 0 0.3
## 285 20.8 1.4 9.1 0.1 1.2 0.0 1.7 0 0.3
## 2019-07-08 2019-07-09 2019-07-10 2019-07-11 2019-07-12 2019-07-13 2019-07-14 2019-07-15 2019-07-16
## 108 1.3 4.8 5.3 14.4 19.3 18.7 8.7 14.9 1.0
## 109 1.7 5.9 6.7 17.0 23.0 20.8 10.1 17.5 1.1
## 110 2.2 6.8 8.1 19.0 26.2 22.4 11.3 19.9 1.0
## 111 1.9 5.9 7.3 16.8 23.0 19.2 10.1 17.8 0.8
## 284 1.1 4.2 4.2 14.3 18.5 20.0 8.9 15.0 1.0
## 285 1.4 4.8 5.4 16.3 21.5 21.6 10.0 17.6 0.9
## 2019-07-17 2019-07-18 2019-07-19 2019-07-20 2019-07-21 2019-07-22 2019-07-23 2019-07-24 2019-07-25
## 108 4.2 6.6 2.6 0 0 0.2 0 9.9 0.9
## 109 5.1 7.9 2.9 0 0 0.4 0 12.1 1.1
## 110 5.9 9.2 3.4 0 0 0.5 0 13.7 1.3
## 111 5.3 8.2 3.0 0 0 0.4 0 12.2 1.2
## 284 3.7 6.0 2.3 0 0 0.1 0 10.4 0.6
## 285 4.4 7.0 2.7 0 0 0.2 0 12.4 0.8
## 2019-07-26 2019-07-27 2019-07-28 2019-07-29 2019-07-30 2019-07-31 2019-08-01 2019-08-02 2019-08-03
## 108 0 0.2 0 0.2 1.3 0.1 0.1 0.3 0.6
## 109 0 0.2 0 0.2 1.5 0.2 0.2 0.3 0.7
## 110 0 0.1 0 0.2 1.7 0.2 0.3 0.4 0.8
## 111 0 0.1 0 0.2 1.4 0.2 0.2 0.2 0.8
## 284 0 0.4 0 0.1 1.4 0.1 0.1 0.4 0.5
## 285 0 0.3 0 0.2 1.7 0.2 0.1 0.4 0.6
## 2019-08-04 2019-08-05 2019-08-06 2019-08-07 2019-08-08 2019-08-09 2019-08-10 2019-08-11 2019-08-12
## 108 2.1 0.1 0.3 0.8 9.4 7.2 7.0 3.9 2.0
## 109 2.5 0.1 0.2 0.9 11.3 9.3 8.2 4.5 2.9
## 110 2.7 0.1 0.3 1.3 13.4 11.4 9.6 5.2 3.5
## 111 2.5 0.2 0.2 1.1 12.5 10.6 8.7 4.8 3.7
## 284 2.0 0.1 0.4 0.4 9.5 6.0 7.0 4.4 2.3
## 285 2.2 0.1 0.4 0.6 10.7 7.3 7.6 4.4 3.8
## 2019-08-13 2019-08-14 2019-08-15 2019-08-16 2019-08-17 2019-08-18 2019-08-19 2019-08-20 2019-08-21
## 108 1.7 1.2 0 8.2 3.6 0.1 9.6 4.2 13.8
## 109 2.1 1.5 0 5.2 4.5 0.0 11.9 5.1 15.8
## 110 2.5 1.9 0 2.3 5.6 0.0 14.3 5.9 18.2
## 111 2.3 1.7 0 0.0 5.3 0.0 13.5 5.3 16.4
## 284 1.4 1.0 0 11.6 3.0 0.1 9.6 3.9 14.6
## 285 1.6 1.2 0 7.9 3.6 0.1 11.1 4.3 15.9
## 2019-08-22 2019-08-23 2019-08-24 2019-08-25 2019-08-26 2019-08-27 2019-08-28 2019-08-29 2019-08-30
## 108 3.5 0 0 3.8 1.2 2.3 1.0 1.8 0.3
## 109 3.6 0 0 4.6 1.6 2.9 1.3 1.9 0.4
## 110 3.7 0 0 5.4 2.0 3.4 1.8 2.1 0.5
## 111 3.2 0 0 4.9 1.9 3.1 1.7 1.8 0.5
## 284 4.6 0 0 3.4 1.3 2.0 0.6 2.2 0.3
## 285 4.7 0 0 3.8 1.7 2.3 0.9 2.2 0.6
## 2019-08-31 2019-09-01 2019-09-02 2019-09-03 2019-09-04 2019-09-05 2019-09-06 2019-09-07 2019-09-08
## 108 7.4 0 7.5 0.7 0.0 0.0 4.1 9.9 4.2
## 109 4.7 0 8.0 0.6 0.1 0.0 4.8 10.5 3.9
## 110 2.0 0 8.9 0.6 0.2 0.0 5.8 11.6 3.7
## 111 0.0 0 8.2 0.3 0.1 0.0 6.1 11.2 3.2
## 284 10.6 0 7.1 0.9 0.0 0.1 4.2 9.7 5.4
## 285 7.1 0 7.6 0.8 0.1 0.0 5.1 9.9 4.8
## 2019-09-09 2019-09-10 2019-09-11 2019-09-12 2019-09-13 2019-09-14 2019-09-15 2019-09-16 2019-09-17
## 108 4.9 0 0 0.2 0 0.1 1.8 13.6 0
## 109 5.6 0 0 0.2 0 0.1 2.0 14.3 0
## 110 6.6 0 0 0.2 0 0.0 2.1 15.4 0
## 111 6.3 0 0 0.2 0 0.0 2.0 13.8 0
## 284 5.2 0 0 0.1 0 0.1 1.7 14.0 0
## 285 6.5 0 0 0.1 0 0.1 1.8 15.1 0
## 2019-09-18 2019-09-19 2019-09-20 2019-09-21 2019-09-22 2019-09-23 2019-09-24 2019-09-25 2019-09-26
## 108 0.1 0.1 0 0 6.6 4.6 4.2 0.5 0.6
## 109 0.1 0.0 0 0 6.5 5.1 4.4 0.3 0.8
## 110 0.0 0.0 0 0 6.8 5.8 4.6 0.1 0.8
## 111 0.0 0.0 0 0 6.1 5.4 4.1 0.0 0.8
## 284 0.2 0.1 0 0 7.5 3.8 4.3 0.9 0.8
## 285 0.1 0.1 0 0 7.5 4.4 4.2 0.7 1.1
## 2019-09-27 2019-09-28 2019-09-29 2019-09-30 2019-10-01 2019-10-02 2019-10-03 2019-10-04 2019-10-05
## 108 0 6.3 0.2 0.1 0 0 0 0.0 0
## 109 0 6.5 0.0 0.1 0 0 0 0.1 0
## 110 0 6.9 0.0 0.1 0 0 0 0.0 0
## 111 0 6.2 0.0 0.1 0 0 0 0.0 0
## 284 0 6.6 0.1 0.1 0 0 0 0.0 0
## 285 0 6.9 0.1 0.1 0 0 0 0.0 0
## 2019-10-06 2019-10-07 2019-10-08 2019-10-09 2019-10-10
## 108 0.1 0.0 3.6 0 0.2
## 109 0.0 0.0 3.1 0 0.2
## 110 0.0 0.1 2.6 0 0.1
## 111 0.0 0.2 2.2 0 0.1
## 284 0.1 0.0 4.4 0 0.4
## 285 0.1 0.0 3.9 0 0.3
Now we are going to read the shapefile in order to filter the dataframe over Victoria state.
wa.map <- readOGR(dsn = "Standard", layer = "VIC_STATE_POLYGON_shp")
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\bruno\Documents\UTS\Statistical Thinking for Data Science\Datasets\Gridded Data Daily Rain\Standard", layer: "VIC_STATE_POLYGON_shp"
## with 346 features
## It has 4 fields
Transforming the dataframe into a SpatialPointsDataFrame
coordinates(daily_rain_df) <- ~ Long + Lat
proj4string(daily_rain_df) <- proj4string(wa.map)
Filtering the dataframe over the state of Victoria. All the latitude and longitude values outside Victoria boundaries will be excluded.
daily_rain_df <- daily_rain_df[!is.na(over(daily_rain_df, as(wa.map, "SpatialPolygons"))), ]
After filtering, we can convert it again to a Data Frame
# Transforming the SpatialPointsDataFrame to Data Frame
daily_rain_df <- as.data.frame(daily_rain_df)
Tidying data, transforming date columns into a single column
daily_rain_df <- as.data.frame(daily_rain_df) %>%
tidyr::gather(key = DATE, value = Rainfall, -Long, -Lat)
# Formating Date field as Date
daily_rain_df$DATE <- as.Date(daily_rain_df$DATE, "%Y-%m-%d")
head(na.omit(daily_rain_df))
## [1] Long Lat DATE Rainfall
## <0 rows> (or 0-length row.names)
Creating a new object to get the avg rainfall values per LONG and LAT
rain_avg_2019 <- daily_rain_df %>%
group_by(Long,Lat) %>%
dplyr::summarize(Mean = mean(Rainfall))
Plotting the lats and longs values with the mean value of rainfall
ggplot() + geom_raster(data = rain_avg_2019, aes(x=rain_avg_2019$Long, y = rain_avg_2019$Lat, fill=Mean)) +
coord_fixed(ratio = 1) +
scale_fill_viridis(direction = -1) +
theme_bw()