#' Add eurostat population numbers check
#'
#' @param df CEIP data frame generated by ceip_totals()
#' @return warning / stop message upon failure
#' @keywords emission, voc, population, data checks
#' @export
check_for_totals <- function(df) {
# check if the data class is correct
if(!any(class(df) == "ceipr_totals")){
if(any(class(df) == "ceipt_data")) {
warning("Data is of class ceipr_data, adding meta_data to this (raw) data is not recommended. Use ceip_totals to convert to totals")
} else {
stop("Data is not of class ceipr_totals, not valid ceipr data!")
}
}
}
#' Add eurostat population numbers to CEIP data frame.
#'
#' Warning: only available from 2007!
#'
#' @param df CEIP data frame generated by ceip_read() (of class ceipr_data)
#' @return Merges the two databases
#' @keywords emission, voc, population
#' @export
ceip_add_population <- function(df) {
# sanity check:
if(!all(c("iso2","year") %in% names(df))) {
stop("Data must contain an iso2 (country code) and year variable.")
}
suppressMessages(
# Load the data set for EU population data
EU_population_data <- eurostat::get_eurostat("tps00001") %>%
# convert the timestamp to a year, for better referencing
dplyr::mutate(year=strtoi(format(time,'%Y')),geo=as.character(geo)) %>%
# rename some variables to make things easier
dplyr::rename(population = values,eurostat=geo) %>%
# add country names and EU status
dplyr::left_join(countrycode::codelist_panel,by=c("year","eurostat")) %>%
# narrow the results down
dplyr::select(iso2 = iso2c,country=country.name.en,population,eu28,year)
)
return(
dplyr::left_join(df, EU_population_data, by = c("iso2","year"))
)
}
#' Calculate per capita emissions
#'
#' @param df CEIP data frame generated by ceip_read() (of class ceipr_data)
#' and supplemented with eurostat data using ceip_add_population()
#' @return Merges the two databases
#' @keywords emission, voc, population
#' @export
ceip_population_emissions <- function(df) {
# sanity check:
if(!all(c("total_emissions","population") %in% names(df))) {
stop("Data does not contain total_emissions and/or population. Did you run ceip_totals and ceip_add_population on your data?")
}
# calculate the emissions per capita
df$emissions_pp <- with(df, 10^6 * total_emissions/population)
# add unit
df$unit_pp <- "g"
return(df)
}
#' Summarize emission totals
#'
#' @param df CEIP data frame generated by ceip_read() (of class ceipr_data)
#' @return a dataframe with emissions per country
#' @export
ceip_totals <- function(df) {
if(!all(c("iso2","year","pollutant","sector_abbr","unit") %in% names(df))) {
stop("Data does not appear to be ceipr_data.")
}
totals <- df %>%
dplyr::group_by(iso2,year,pollutant,sector,sector_abbr,unit) %>%
dplyr::summarise(total_emissions = sum(emission))
class(totals) <- c(class(totals),"ceipr_totals")
return(totals)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.