Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Created January 8, 2023 14:17
Show Gist options
  • Save tonyelhabr/9617114eccd9508d17e8acc95377c3ca to your computer and use it in GitHub Desktop.
Save tonyelhabr/9617114eccd9508d17e8acc95377c3ca to your computer and use it in GitHub Desktop.
Austin precipitation by day of week between 2022-10-01 and 2022-12-31

Data source

library(httr)
library(tibble)
library(purrr)
library(readr)
library(dplyr)
library(lubridate)

resp <- GET(
  'https://api.open-meteo.com/v1/forecast?latitude=30.27&longitude=-97.74&hourly=precipitation&temperature_unit=fahrenheit&precipitation_unit=inch&timezone=America%2FChicago&start_date=2022-10-01&end_date=2022-12-31'
)

hourly_content <- content(resp)[['hourly']]

df <- tibble(
  time = hourly_content |> 
    pluck('time') |> 
    flatten_chr() |> 
    parse_datetime(),
  precip = hourly_content |> 
    pluck('precipitation') |> 
    flatten_dbl()
)

agg <- df |> 
  mutate(
    date = date(time),
    dow = wday(time, label = TRUE, abbr = TRUE)
  ) |> 
  group_by(date, dow) |>
  summarize(
    days_gt0 = sum(any(precip > 0)),
    across(precip, sum)
  ) |> 
  ungroup() |> 
  group_by(dow) |> 
  summarize(
    across(
      c(days_gt0, precip),
      sum
    )
  ) |> 
  ungroup()
agg
#> # A tibble: 7 × 3
#>   dow   days_gt0 precip
#>   <ord>    <int>  <dbl>
#> 1 Sun          3  0.224
#> 2 Mon          8  2.87 
#> 3 Tue          4  0.42 
#> 4 Wed          1  0.016
#> 5 Thu          4  0.368
#> 6 Fri          7  1.73 
#> 7 Sat          3  1.55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment