R/lists.r

Defines functions wpp_countries survey_countries list_surveys

Documented in list_surveys survey_countries wpp_countries

#' List all surveys available for download
#'
#' @return character vector of surveys
#' @importFrom oai list_records
#' @autoglobal
#' @examples
#' \dontrun{
#'   list_surveys()
#' }
#' @export
list_surveys <- function() {

  record_list <-
    data.table(list_records("https://zenodo.org/oai2d",
      metadataPrefix = "oai_datacite",
      set = "user-social_contact_data"
    ))
  ## find common DOI for different versions, i.e. the "relation" that is a DOI
  relations <- grep("^relation(\\.|$)", colnames(record_list), value = TRUE)
  DOIs <- apply(record_list, 1, function(x) grep("^doi:.*zenodo", x[relations], value = TRUE))
  record_list <- record_list[, common_doi := DOIs]
  record_list <- record_list[, url := sub("doi:", "https://doi.org/", common_doi)]
  ## get number within version DOI, this is expected to be ascending by version
  record_list <-
    record_list[, doi.nb := as.integer(sub("^.*zenodo\\.", "", identifier.1))]
  ## save date at which first entered
  record_list <- record_list[, date := min(date), by = common_doi]
  ## order by DOI number and extract newest version
  record_list <- record_list[order(-doi.nb)]
  record_list <- record_list[, .SD[1], by = common_doi]
  ## order by date
  setkey(record_list, date)
  return(record_list[, list(date_added = date, title, creator, url = url)])
}

#' List all countries contained in a survey
#'
#' @param country.column column in the survey indicating the country
#' @param ... further arguments for [get_survey()]
#' @return list of countries
#' @inheritParams get_survey
#' @examples
#' data(polymod)
#' survey_countries(polymod)
#' @export
survey_countries <- function(survey, country.column = "country", ...) {
  survey <- get_survey(survey, ...)
  return(as.character(unique(survey[["participants"]][[country.column]])))
}

#' List all countries and regions for which socialmixr has population data
#'
#' Uses the World Population Prospects data from the `wpp2017` package
#' @return list of countries
#' @import wpp2017
#' @importFrom data.table fread setkey
#' @importFrom utils data
#' @importFrom countrycode countrycode
#' @autoglobal
#' @examples
#' wpp_countries()
#' @export
wpp_countries <- function() {

  popF <- fread(system.file("data", "popF.txt", package = "wpp2017"))
  popM <- fread(system.file("data", "popM.txt", package = "wpp2017"))

  countries <- unique(c(popF$country_code, popM$country_code))
  found_countries <-
    suppressWarnings(countrycode::countrycode(
      countries,
      "un",
      "country.name"
    ))
  found_countries <- found_countries[!is.na(found_countries)]
  return(found_countries)
}

Try the socialmixr package in your browser

Any scripts or data that you put into this service are public.

socialmixr documentation built on Oct. 29, 2022, 1:09 a.m.