#' Life Tables
#'
#' This function allows you to easily download personalized Life Tables from the Eurostat website.
#' @param geo Select the state: AL AT BE BG CH CY CZ DE DE_TOT DK EA18 EA19 EE EEA30 EEA31 EFTA EL ES EU27 EU28 FI FR HR HU IE IS IT LI LT LU LV ME MK MT NL NO PL PT RO RS SE SI SK TR UK AZ BY GE RU UA AD FX MD SM AM
#' @param sex Select the gender: M F T
#' @param time Select the year from 1960
#' @param age Select the age from 0 to 85
#' @param indic_de Select the demographic indicator: Mx (Age specific death rate),ex (Life expectancy at given exact age),qx (Probability of dying between ages), px (Probability of surviving between exact ages), Lx (Person-years lived between exact age), lx (Number left alive at given exact age), Tx (Total person-years lived above given exact age)
#' @keywords life tables
#' @export
#' @examples
#' lT <- lifeTable(geo="IT",sex="M",time=2000,indic_de="px")
lifeTable <- function(geo=NULL,sex=NULL,time=NULL,age=NULL,indic_de=NULL) {
require(rvest)
require(stringr)
require(eurostat)
id <- "demo_mlifetable"
dat <- get_eurostat(id, time_format = "num")
dat$values <- as.numeric(dat$values)
if(!is.null(geo)){
dat <- dat[dat$geo==geo,]
dat$geo <- NULL
}
if(!is.null(sex)){
dat <- dat[dat$sex==sex,]
dat$sex <- NULL
}
dat$time <- as.numeric(dat$time)
if(!is.null(time)){
dat <- dat[dat$time==time,]
dat$time <- NULL
}
flag <- NULL
if(!is.null(age)){
flag <- 1
}
dat$age <- str_sub(dat$age,2,str_length(dat$age))
dat$age[dat$age=="_LT1"] <- "0"
dat$age[dat$age=="_GE85"] <- "85"
dat$age <- as.numeric(dat$age)
dat <- dat[order(dat$age,decreasing=FALSE),]
if(!is.null(flag)){
dat <- dat[dat$age==age,]
dat$age <- NULL
}
levels(dat$indic_de) <- c("Mx","ex","qx","px","Lx","lx","Tx")
if(!is.null(indic_de)){
dat <- dat[dat$indic_de==indic_de,]
dat$indic_de <- NULL
colnames(dat)[colnames(dat)=="values"] <- indic_de
}
return(dat)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.