Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # skeleton of a function that collects lifetables, exposures, death counts,
- # and stacks all countries, years, sexes
- # package can be pain to install, try different stuff, seek help
- library(HMDHFDplus)
- Countries <- getHMDcountries() # returns vector of HMD country codes
- # us <- "HMD username" # change to yours: you need to be registered at www.mortality.org
- # pw <- "HMD password" # change to yours
- # makes long data frame of HMD lifetables, takes several minutes to run
- LT <- do.call(rbind, # (idiom to rbind a list of data.frames together)
- # lapply() is like looping, except it sticks everything in a lsit for you,
- # sometimes also a bit faster, and usually more elegant...
- lapply(Countries, function(XYZ, us, pw){ # here define an 'anonymous' function.
- # print progress to console
- cat(XYZ,"\n")
- # male lifetables
- LTM <- readHMDweb(XYZ, "mltper_1x1", username = us, password = pw)
- # female lifetable
- LTF <- readHMDweb(XYZ, "fltper_1x1", username = us, password = pw)
- # add sex columns
- LTM$Sex <- "m"
- LTF$Sex <- "f"
- # ADD CODE HERE to add death count and exposure columns
- # after having read in those data objects
- # then stick together
- LT <- rbind(LTM, LTF)
- # add country name
- LT$CNTRY <- XYZ
- LT
- }, us = us, pw = pw))
- # then the rest of the assignment basically follow from this object.
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement