r - Aggregate data frame to coarser spatial resolution -
i have dataset 0.25 * 0.25 degree grid resolution. i've 1 * 1 degree resolution , want make them comparable (both changed 1 * 1 resolution). area 28.5 36.5 longitude , -4.5 4.5 latitude monthly data 2003 2012.i've attached first few lines e.g. i've attached excel file of full data if below not help! https://onedrive.live.com/redir?resid=9e74848e574367c6!2625&authkey=!aocpfttyhvqu8ca&ithint=file%2ccsv i've been looking 'sp' (aggregate) , 'raster' packages cannot figure out. appreciated always!
> head(cur.data, 40) longitude latitude year month decimdate rainfall rainanom 1 28.625 -4.375 2012 12 2012.917 173.69343 73.74917 2 28.875 -4.375 2012 12 2012.917 186.91148 86.96723 3 29.125 -4.375 2012 12 2012.917 158.17921 58.23495 4 29.375 -4.375 2012 12 2012.917 194.71006 94.76581 5 29.625 -4.375 2012 12 2012.917 160.84774 60.90349 6 29.875 -4.375 2012 12 2012.917 192.01070 92.06645 7 30.125 -4.375 2012 12 2012.917 225.45180 125.50755 8 30.375 -4.375 2012 12 2012.917 234.98836 135.04410 9 30.625 -4.375 2012 12 2012.917 182.77813 82.83388 10 30.875 -4.375 2012 12 2012.917 226.08477 126.14052 11 31.125 -4.375 2012 12 2012.917 271.90741 171.96316 12 31.375 -4.375 2012 12 2012.917 272.48264 172.53839 13 31.625 -4.375 2012 12 2012.917 265.44591 165.50166 14 31.875 -4.375 2012 12 2012.917 266.44458 166.50033 15 32.125 -4.375 2012 12 2012.917 270.94755 171.00329 16 32.375 -4.375 2012 12 2012.917 257.85288 157.90863 17 32.625 -4.375 2012 12 2012.917 265.42979 165.48554 18 32.875 -4.375 2012 12 2012.917 255.23151 155.28726 19 33.125 -4.375 2012 12 2012.917 249.32812 149.38387 20 33.375 -4.375 2012 12 2012.917 165.93709 65.99284 21 33.625 -4.375 2012 12 2012.917 165.71675 65.77250 22 33.875 -4.375 2012 12 2012.917 201.09573 101.15148 23 34.125 -4.375 2012 12 2012.917 216.02753 116.08328 24 34.375 -4.375 2012 12 2012.917 255.53255 155.58830 25 34.625 -4.375 2012 12 2012.917 202.72544 102.78119 26 34.875 -4.375 2012 12 2012.917 71.00399 -28.94026 27 35.125 -4.375 2012 12 2012.917 69.55960 -30.38465 28 35.375 -4.375 2012 12 2012.917 84.89567 -15.04858 29 35.625 -4.375 2012 12 2012.917 144.54023 44.59598 30 35.875 -4.375 2012 12 2012.917 227.81630 127.87205 31 36.125 -4.375 2012 12 2012.917 193.73762 93.79337 32 36.375 -4.375 2012 12 2012.917 156.49207 56.54782 33 28.625 -4.125 2012 12 2012.917 182.18007 82.23582 34 28.875 -4.125 2012 12 2012.917 208.52020 108.57595 35 29.125 -4.125 2012 12 2012.917 175.20243 75.25817 36 29.375 -4.125 2012 12 2012.917 166.91644 66.97218 37 29.625 -4.125 2012 12 2012.917 156.38420 56.43994 38 29.875 -4.125 2012 12 2012.917 167.72313 67.77888 39 30.125 -4.125 2012 12 2012.917 175.48325 75.53899 40 30.375 -4.125 2012 12 2012.917 210.74390 110.79965
i think raster
package way go:
require(raster) cur.data <- read.csv("lvb_trmm_monthly_rainfall.csv") r <- cur.data coordinates(r) <- ~ longitude + latitude gridded(r) <- true # stack each variable band in raster: tmp <- raster(r, layer = 1) (i in 2:5) { tmp <- stack(tmp, raster(r, layer = i)) } r <- tmp # aggregate 1 degree resolution (4 times coarse): r <- aggregate(r, fact = 4, fun = mean) # dataframe coords <- as.matrix(coordinates(r)) df <- data.frame(longitude = coords[, 1], latitude = coords[, 2], as.data.frame(r))
this gives:
longitude latitude year month decimdate rainfall rainanom 1 29 1 2003 1 2003 60.69725 -39.24700 2 30 1 2003 1 2003 39.60284 -60.34141 3 31 1 2003 1 2003 34.12878 -65.81548 4 32 1 2003 1 2003 46.32009 -53.62416 5 33 1 2003 1 2003 71.27627 -28.66799 6 34 1 2003 1 2003 82.92516 -17.01909
however, see have data different time periods. perhaps want repeat above steps each time period.
Comments
Post a Comment