I have a data.table with observations of temp and other weather info over 15 days for several sites. This dput
is for all observations for two sites.
library(data.table)
structure(list(site = c("100", "100", "100", "100", "100", "100",
"100", "100", "100", "100", "100", "100", "100", "100", "100"
), precursor_date = structure(c(15203, 15202, 15201, 15200, 15199,
15198, 15197, 15196, 15195, 15194, 15193, 15192, 15191, 15190,
15189), class = "Date"), lat = c(46.864, 46.864, 46.864, 46.864,
46.864, 46.864, 46.864, 46.864, 46.864, 46.864, 46.864, 46.864,
46.864, 46.864, 46.864), lon = c(-67.998, -67.998, -67.998, -67.998,
-67.998, -67.998, -67.998, -67.998, -67.998, -67.998, -67.998,
-67.998, -67.998, -67.998, -67.998), origDate = structure(c(15204,
15204, 15204, 15204, 15204, 15204, 15204, 15204, 15204, 15204,
15204, 15204, 15204, 15204, 15204), class = "Date"), last = c(2011,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011,
2011, 2011, 2011), begin = c(2011, 2011, 2011, 2011, 2011, 2011,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011), precursor_day_labl = structure(1:15, .Label = c("obd_p1",
"obd_p2", "obd_p3", "obd_p4", "obd_p5", "obd_p6", "obd_p7", "obd_p8",
"obd_p9", "obd_p10", "obd_p11", "obd_p12", "obd_p13", "obd_p14",
"obd_p15"), class = "factor"), year = c(2011, 2011, 2011, 2011,
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011
), yday = c(229, 228, 227, 226, 225, 224, 223, 222, 221, 220,
219, 218, 217, 216, 215), dayl = c(50112, 50457.6015625, 50457.6015625,
50803.19921875, 50803.19921875, 51148.80078125, 51148.80078125,
51494.3984375, 51494.3984375, 51840, 51840, 52185.6015625, 52185.6015625,
52531.19921875, 52531.19921875), prcp = c(0, 17, 5, 4, 6, 6,
13, 8, 0, 16, 14, 6, 0, 0, 7), srad = c(403.200012207031, 176,
249.600006103516, 288, 297.600006103516, 268.799987792969, 179.199996948242,
192, 406.399993896484, 208, 227.199996948242, 307.200012207031,
371.200012207031, 304, 182.399993896484), swe = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tmax = c(22.5, 20.5, 24.5,
26.5, 25, 22.5, 20.5, 21, 24, 23.5, 25, 28, 24, 23.5, 22), tmin = c(10.5,
14.5, 15, 14.5, 12.5, 12, 14, 14, 11, 15.5, 16, 14, 12.5, 14.5,
15), vp = c(1280, 1640, 1720, 1640, 1440, 1400, 1600, 1600, 1320,
1760, 1800, 1600, 1440, 1640, 1720), satv = c(19.99250234375,
17.77504867875, 22.44414580875, 25.14497571375, 23.09540625,
19.99250234375, 17.77504867875, 18.30827693, 21.80845952, 21.18811306125,
23.09540625, 27.34314816, 21.80845952, 21.18811306125, 19.41676944
), r_hum = c(64.024001497749, 92.2641636397099, 76.6346830330004,
65.2217770527891, 62.3500614976193, 70.026251638163, 90.0138181850829,
87.3921672758967, 60.526971141151, 83.0654431053979, 77.9375768720241,
58.5155736507555, 66.0294230630738, 77.4018901663935, 88.5832221119478
)), class = c("data.table", "data.frame"), row.names = c(NA,
-15L), .internal.selfref = <pointer: 0x000001b632fd1ef0>)
I want to get the mean of the weather data (prcp
, tmax
, tmin
, r_hum
) for each #-day interval of the 15 days moving backward in time from the start day, which I called origDate
in the DT
. The dates that go into each respective fifteen day window for each site is under precursor_date
. There would only be one 2-day mean, one 3-day mean, one 4-day mean, etc. and those would be based on the respective #-day window that immediately preceded the origDate
. For example, if the start day is 2011-08-18, I want the mean for 2 days before 8-18( 08-17 and 08-16), 3 days (08-17, 08-16, 08-15), etc., up to the largest window, 15-day (08-17 through 08-03). I don't need every small interval mean possible in the 15 day window. Just the one that immediately precedes origDate
.
To give an idea of what the subset I want, in dplyr
, I can use
df %>% group_by(site) %>% slice_head(n= x)
# A tibble: 2,556 x 19
# Groups: site [1,278]
site precursor_date lat lon origDate last begin precursor_day_l~ year yday dayl prcp srad swe tmax
<chr> <date> <dbl> <dbl> <date> <dbl> <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 100 2011-08-17 46.9 -68.0 2011-08-18 2011 2011 obd_p1 2011 229 50112 0 403. 0 22.5
2 100 2011-08-16 46.9 -68.0 2011-08-18 2011 2011 obd_p2 2011 228 50458. 17 176 0 20.5
3 101 2011-08-11 44.3 -72.7 2011-08-12 2011 2011 obd_p1 2011 223 50458. 8 272 0 26
4 101 2011-08-10 44.3 -72.7 2011-08-12 2011 2011 obd_p2 2011 222 50803. 25 253. 0 26.5
5 102 2011-08-21 46.5 -68.0 2011-08-22 2011 2011 obd_p1 2011 233 49421. 0 378. 0 27
6 102 2011-08-20 46.5 -68.0 2011-08-22 2011 2011 obd_p2 2011 232 49421. 1 397. 0 28
where x is the number of days I want to subset from each group before getting means. But if I use
df %>% group_by(site) %>% slice_head(n= x) %>% mean(prcp)
I get an error and I don't know why. The error is
Warning message:
In mean.default(., "prcp") :
argument is not numeric or logical: returning NA
While I don't know why that error occurs, I'm more intent on getting the subsets to work within a data.table
. The columns that I want subset means for are prcp, tmax, tmin, and r_hum. I would end up creating 60 new columns, 15 for each weather variable. And a lot of the columns would have NA or something, since the DT has the daily observations in columns. To give an idea of what the output might look like, here is a mock-up. It doesn't have to look this way, as long as I have the means for each weather variable and time window in the DT aligned with the appropriate site.
site precursor_date lat lon origDate ... prcp2dmean prcp3dmean prcp4dmean ... tmax2dmean tmax3dmean ...
100 2011-08-17 46.864 -67.998 2011-08-18 ... 1.2 1.4 1.4 ... 25 24 ...
100 2011-08-16 46.864 -67.998 2011-08-18 ... 1.2 1.4 1.4 ... 25 24 ...
100 2011-08-15 46.864 -67.998 2011-08-18 ... NA 1.4 1.4 ... NA 24 ...
100 2011-08-14 46.864 -67.998 2011-08-18 ... NA NA 1.4 ... NA NA ...
100 2011-08-13 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-12 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-11 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-10 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-09 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-08 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-07 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-06 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-05 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-04 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
100 2011-08-03 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-17 46.864 -67.998 2011-08-18 ... 1.2 1.4 1.4 ... 25 24 ...
10 2011-08-16 46.864 -67.998 2011-08-18 ... 1.2 1.4 1.4 ... 25 24 ...
10 2011-08-15 46.864 -67.998 2011-08-18 ... NA 1.4 1.4 ... NA 24 ...
10 2011-08-14 46.864 -67.998 2011-08-18 ... NA NA 1.4 ... NA NA ...
10 2011-08-13 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-12 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-11 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-10 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-09 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-08 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-07 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-06 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-05 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-04 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
10 2011-08-03 46.864 -67.998 2011-08-18 ... NA NA NA ... NA NA ...
In my DT I tried
pi_df5[, pi_df5 %>% slice_head(n=2) %>% mean(prcp), by = site]
But that doesn't work.