Exercises


EXERCISE 1 : Find the mean and standard deviation of licensed geothermal productions in all years. (Tip: Use lubridate::year to get years from date data.)

df %>% group_by(year = year(dt)) %>% summarise(mean_geo = mean(geothermal_lic), sd_geo = sd(geothermal_lic))


EXERCISE 2 : Find the hourly average unlicensed solar (sun_ul) production levels for May 2020.

df %>% filter(dt >= "2020-05-01" & dt <= "2020-05-31") %>% group_by(hour = hour(dt)) %>% summarise(
  avg_prod = mean(sun_ul)) %>% mutate(across(where(is.numeric), ~ round(., 2)))


EXERCISE 3 : Find the average daily percentage change of licensed biomass (biomass_lic) in 2019. (e.g. Suppose daily production is 50 in day 1 and 53 in day 2, then the change should be (53-50)/50 -1 = 0.06) (Tip: Use lubridate::as_date to convert date time to date. Use lag and lead functions to offset values.)

df %>% mutate(dt = date(dt)) %>% filter(dt >= "2019-01-01" & dt < "2020-01-01") %>% select(dt, biomass_lic) %>%
  arrange(desc(dt)) %>% group_by(dt) %>% summarise(sum_daily = sum(biomass_lic)) %>% transmute(
  dt, sum_daily, prev_day = lag(sum_daily,1)) %>% transmute(sum_daily, prev_day, daily_chng = ((
  sum_daily - prev_day) / prev_day)) %>% summarise(average_change = round(mean(daily_chng, na.rm=TRUE), digits = 5))


EXERCISE 4 : Find the yearly total production levels in TWh (Current values are in MWh. 1 GWh is 1000 MWh and 1 TWh is 1000 GWh). (Tip: In order to avoid a lengthy summation you can use tidyr::pivot_longer to get a long format.)

df %>% group_by(year = year(dt)) %>% rowwise() %>% summarise(total_p = sum(c_across(where(is.numeric)))) %>% summarise(total = sum(total_p) / 1000000) %>% transmute(year, total_production = round(total, digits = 1))
`summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
LS0tDQp0aXRsZTogIioqQkRBLTUwMyBXZWVrIDMgLSBSZW5ld2FibGUgRXhlcmNpc2VzIFRhc2sqKiINCmF1dGhvcjogIl9FbWlyaGFuIMWeYWhpbl8iDQpkYXRlOiAiYHIgZm9ybWF0KFN5cy50aW1lKCksICclQiAlZCwgJVknKWA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