0

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) 
Kendall A
  • 1
  • 1
  • 1
    Welcome to SO! Please read this and try to make your problem reproducible so others can help https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tung Nov 15 '19 at 19:03

1 Answers1

1

This might work for your case. First we need to create a Period column based on the time range (case_when). Then do the summation based on a group of two columns DATE and Period (summarise_at).

library(dplyr)

prec_sum <- prec_data %>% 
  select(DATE, TIME, starts_with("PREC")) %>% 
  mutate(TIME = as.numeric(TIME)) %>% 
  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()
Tung
  • 26,371
  • 7
  • 91
  • 115
  • I'm getting this error : Error: Not compatible with requested type: [type=character; target=double]. I've only modified to include the dataset name: 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() – Kendall A Nov 15 '19 at 19:38
  • Please post the sample of your dataset per the instruction in the link I posted. It's closet to impossible to know why error happened without seeing the data – Tung Nov 15 '19 at 19:46
  • Have a look now – Kendall A Nov 15 '19 at 20:11
  • Better but not yet reproducible. See this example https://stackoverflow.com/q/56811184/786542. Make sure that others can copy and paste the code then run it on a different computer. – Tung Nov 15 '19 at 20:41