将数据框架聚合到更粗糙的空间分辨率

时间:2015-07-13 18:35:21

标签: r spatial raster sp

我有一个0.25 * 0.25度网格分辨率的数据集。我是另一个1 * 1度分辨率,并希望使它们具有可比性(均改为1 * 1分辨率)。该区域为28.5至36.5经度和-4.5至4.5纬度,具有2003年至2012年的月度数据。我已将前几行作为例如如果以下内容无效,我已附上完整数据的excel文件! https://onedrive.live.com/redir?resid=9E74848E574367C6!2625&authkey=!AOcpftTyHVqU8CA&ithint=file%2ccsv我一直在调查' sp' (聚合)和'光栅'包但无法弄清楚这一点。任何帮助一如既往的赞赏!

> 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 

1 个答案:

答案 0 :(得分:4)

我认为raster包是可行的方法:

require(raster)

cur.data <- read.csv("LVB_TRMM_Monthly_Rainfall.csv")

r <- cur.data
coordinates(r) <- ~ Longitude + Latitude
gridded(r) <- TRUE

# Stack each variable as a band in the raster:
tmp <- raster(r, layer = 1)
for (i in 2:5) {
  tmp <- stack(tmp, raster(r, layer = i))
}
r <- tmp

# Aggregate to 1 degree resolution (4 times as coarse):
r <- aggregate(r, fact = 4, fun = mean)

# Get it back to a dataframe
coords <- as.matrix(coordinates(r))
df <- data.frame(Longitude = coords[, 1], Latitude = coords[, 2],
                 as.data.frame(r))

这给出了:

  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

但是,我发现您有来自不同时间段的数据。也许您会想要在每个时间段重复上述步骤。