readHFCweb: readHFCweb get HFC data straight from the web into R!

View source: R/readHFD.R

readHFCwebR Documentation

readHFCweb get HFC data straight from the web into R!

Description

This concatenates the necessary url and calls read.csv() with all the necessary defaults to avoid annoying surprises. Age is given as an integer, along with an AgeInterval. The default behavior is to change the AgeInterval column to character and produce a logical indicator column, OpenInterval. Year is also given as the starting year of a YearInterval, rather than the original Year1 and Year2 columns. The column Country is renamed CNTRY. All other original columns are maintained. Output is invisibly returned, so you must assign it to take a look.

Usage

readHFCweb(CNTRY, item, fixup = TRUE, ...)

Arguments

CNTRY

character string of the HCD short code. Only one! Run getHFCcountries(FALSE) to see what the options are.

item

character string of the data product code, which is the base file name, but excluding the country code and file extension .txt. For instance, "ASFRstand_TOT", "ASFRstand_BO", "TFRMAB_TOT", "TFRMAB_BO". Only one item!

fixup

logical. Default TRUE. Should column classes be coerced to those more similar to HFD, HMD?

...

optional arguments passed to read.csv(). Not required.

Examples

## Not run: 
DF <- readHFCweb("CZE","TFRMAB_TOT")
head(DF)
DF <- readHFCweb("CZE","ASFRstand_BO")
head(DF)

# get ASFRstand_BO for all countries where available:
Countries <- getHFCcountries()
# takes a minute to run

urls <- paste0("http://www.fertilitydata.org/data/", 
               Countries,"/", Countries, "_",  "ASFRstand_BO", ".txt")

HaveBO <- RCurl::url.exists(urls)
 # we grab data for these countries:
 (Countries <- Countries[HaveBO])

# Also takes 1-15 min depending on internet connection and machine
# read in one at a time and stick together into long data.frame
allBO <- do.call(rbind,
        # this is the iteration of reading in
        lapply(Countries, function(CNTRY){
            readHFCweb(CNTRY, item = "ASFRstand_BO")
             })) # closes off the meta-rbind thingy
dim(allBO) # [1] 133237     31
unique(allBO$CNTRY)


## End(Not run)


HMDHFDplus documentation built on Feb. 16, 2023, 8:34 p.m.