R/combine.R

Defines functions combine_wash assign_bgd_corp

Documented in assign_bgd_corp combine_wash

################################################################################
#
#' Combine all WASH indicator sets
#'
#' @param admin Administrative dataset
#' @param demo Demographic indicators dataset
#' @param poverty Poverty indicators dataset
#' @param water Water indicators dataset
#' @param sanitation Sanitation indicators dataset
#' @param handwash Handwashing indicators dataset
#' @param hygiene Hygiene indicators dataset
#' @param overall Overall indicators dataset
#' @return A data frame containing all WASH indicators
#' @examples
#' # apply combine_wash() to surveyDataBGD dataset
#' 
#' ppiMatrixBGD <- washdata::ppiMatrixBGD 
#' 
#' admin <- get_admin_vars(data = washdata::surveyDataBGD)
#' demo <- get_demo_vars(data = washdata::surveyDataBGD)
#' poverty <- get_poverty_vars(surveyData = washdata::surveyDataBGD, 
#'                             index = washdata::surveyDataBGD$ppi, 
#'                             ccode = "BGD",
#'                             ppiTable = ppitables::ppiBGD2013)
#' water <- get_water_vars(surveyData = washdata::surveyDataBGD)
#' sanitation <- get_sanitation_vars(surveyData = washdata::surveyDataBGD)
#' handwash <- get_handwashing_vars(surveyData = washdata::surveyDataBGD)
#' hygiene <- get_hygiene_vars(surveyData = washdata::surveyDataBGD)
#' overall <- get_overall_vars(adminDF = admin, waterDF = water, sanDF = sanitation)
#' 
#' indicatorsDF <- combine_wash(admin = admin, demo = demo, poverty = poverty,
#'                              water = water, sanitation = sanitation,
#'                              handwash = handwash, hygiene = hygiene,
#'                              overall = overall)
#' 
#' @export
#'
#
################################################################################

combine_wash <- function(admin, demo, poverty, water, 
                         sanitation, handwash, hygiene, overall) {
  ## Merge all data.frames from current survey data
  temp <- merge(admin, demo, by = "uniqueID")
  temp <- merge(temp, poverty, by = "uniqueID")
  temp <- merge(temp, water, by = "uniqueID")
  temp <- merge(temp, sanitation, by = "uniqueID")
  temp <- merge(temp, handwash, by = "uniqueID")
  temp <- merge(temp, hygiene, by = "uniqueID")
  indicatorsDF <- merge(temp, overall, by = "uniqueID")
  ## Return output
  return(indicatorsDF)
}


################################################################################
#
#' Assign Dhaka north or south corporation identifiers to the dataset from
#' Bangladesh
#'
#' @param indicators A data frame of calculated indicators
#' @param corporations A SpatialPolygonsDataFrame delineating areas of Dhaka
#'     belonging to North and South corporations.
#' @param crs A character value specifying the coordinate reference system (CRS)
#'     to be used. Default is longlat projection datum WGS84.
#' @return A character vector with length equal to \code{indicators} specifying
#'     location of data (i.e., "North City Corporation", "South City Corporation", 
#'     "Outside")
#' @examples
#' 
#' ppiMatrixBGD <- washdata::ppiMatrixBGD
#' 
#' admin <- get_admin_vars(washdata::surveyDataBGD)
#' demo <- get_demo_vars(washdata::surveyDataBGD)
#' poverty <- get_poverty_vars(surveyData = washdata::surveyDataBGD, 
#'                             index = washdata::surveyDataBGD$ppi, 
#'                             ccode = "BGD",
#'                             ppiTable = ppitables::ppiBGD2013)
#' water <- get_water_vars(washdata::surveyDataBGD)
#' sanitation <- get_sanitation_vars(washdata::surveyDataBGD)
#' handwash <- get_handwashing_vars(washdata::surveyDataBGD)
#' hygiene <- get_hygiene_vars(washdata::surveyDataBGD)
#' overall <- get_overall_vars(adminDF = admin, waterDF = water, sanDF = sanitation)
#' 
#' indicatorsDF <- combine_wash(admin = admin, demo = demo, poverty = poverty,
#'                              water = water, sanitation = sanitation,
#'                              handwash = handwash, hygiene = hygiene,
#'                              overall = overall)
#' 
#' indicatorsDF <- assign_bgd_corp(indicators = indicatorsDF, 
#'                                 corporations = corporations)
#' 
#' @export
#'
#
################################################################################

assign_bgd_corp <- function(indicators, corporations,
                            crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0") {
  ## Convert map object to longlat CRS
  xx <- sp::spTransform(x = corporations, CRSobj = sp::CRS(crs))                          
  ##
  yy <- sp::SpatialPointsDataFrame(coords = indicators[ , c("longitude", "latitude")],
                                   data = indicators,
                                   proj4string = sp::CRS(crs))
  ##
  north <- subset(xx, corprtn == "north")
  south <- subset(xx, corprtn == "south")
  ##
  zz <- ifelse(indicators$psu %in% raster::intersect(yy, north)@data$psu, "North City Corporation",
          ifelse(indicators$psu %in% raster::intersect(yy, south)@data$psu, "South City Corporation", "Outside"))
  ##
  indicators$corporation <- zz
  ## Return output
  return(indicators)
}
validmeasures/wsup documentation built on Dec. 16, 2019, 4:50 a.m.