I'm working with a simple dataset. It contains three variables of interest. 1. Date YYYY-MM-DD 2. Hourly (##) 3. Precip_H (#.##).
My situation is that I am trying to find code that will for example, sum the precip_H values across rows that are equal to a specific DATE and are within 00-11 for the value of Hourly. Then the next set would do all the same but for 12-23 range in Hourly.
This dataset is a weather station that reports precipitation hourly. What I am trying to do is use that information to make two 12 hour precipitation values per day across all days of the dataset.
DATE TIME PRECIP_H DATEyyyy DATEmm DATEdd
<date> <chr> <dbl> <dbl> <dbl> <dbl>
1 2019-06-05 17 0 2019 6 5
2 2019-06-01 20 0 2019 6 1
3 2019-06-06 19 0 2019 6 6
4 2019-05-27 00 0 2019 5 27
5 2019-08-25 20 0 2019 8 25
6 2019-08-08 04 0 2019 8 8
7 2019-09-01 07 0 2019 9 1
8 2019-07-18 21 0 2019 7 18
9 2019-06-18 23 0 2019 6 18
10 2019-08-11 12 0 2019 8 11
library(readxl)
precip2019 <- Clean_Chicago_Midway_Precp_Hourly_2019_1945790 <- read_excel("S:/Natural Resources/Staff/Beach Management/Beaches Main/+ DATA ANALYSIS +/Beaches 2019/Master Files/Clean_Chicago Midway Precp Hourly 2019_1945790.xlsx")
names(precip2019)[names(precip2019) == "HourlyPrecipitation"] <- "PRECIP_H"
precip2019$DATE <- as.Date(precip2019$DATE, format = '%Y-%m-%d')
precip2019$DATEyyyy <- as.numeric(format(precip2019$DATE, '%Y'))
precip2019$DATEmm <- as.numeric(format(precip2019$DATE, '%m'))
precip2019$DATEdd <- as.numeric(format(precip2019$DATE, '%d'))
prec_sum <- precip2019 %>%
select(DATE, TIME, starts_with("PREC")) %>%
mutate(Period = case_when(between(TIME, 0, 11) ~ "1st_half",
TRUE ~ "2nd_half")) %>%
group_by(DATE, Period) %>%
summarise_at(vars(starts_with("PREC")), list(~ sum(., na.rm = TRUE))) %>%
ungroup()
View(prec_sum)