R/AdmHouseholdReport.R

Defines functions AdmHouseholdReport

Documented in AdmHouseholdReport

#' Generate VDC house Statistics
#'
#' Pulls entire VDC house counts with labels
#' @param code input string of admin code
#' @param flag string either vdcmun, district, or ward 
#' @param house object from dbReadTable
#' @return list of dataframes containing counts and labels
#' @export
#' @examples
#' AdmhouseReport("524 1 02 07 3 015", vdcmun, house)

AdmHouseholdReport <- function(code, flag, house) {

  if (flag == "vdcmun") {
    group <- colnames(house)
    hinf <- lapply(group, function(x, vc) {
      house %>%
      filter(hrrp_vcode == vc)  %>%
      group_by_("hrrp_vcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(hinf) <- group
  }

  else if (flag == "district") {
    group <- colnames(house)
    hinf <- lapply(group, function(x, vc) {
      house %>%
      filter(hrrp_dcode == vc)  %>%
      group_by_("hrrp_dcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(hinf) <- group
  }

  else {
    group <- colnames(house)
    hinf <- lapply(group, function(x, vc) {
      house %>%
      filter(hrrp_wcode == vc)  %>%
      group_by_("hrrp_wcode", x) %>%
      summarise(count = n()) 
    }, vc = code)
    names(hinf) <- group
  }

  #Getting labels
	ovals <- lapply(c(1:length(group)), function(x) hinf[[x]][[2]])
	house_info <- mapply(labelValue, group, ovals, "household")

  #Label handling, messy but working
  hi <- lapply(c(1:length(group)), function(x, b, bi) {
      if (anyNA(hinf[[x]][2]) == TRUE & (nrow(hinf[[x]][2]) > 1) == TRUE) 
          append(house_info[[x]], "Not Present")
        else if (length(house_info[[x]] > 0))
          house_info[[x]]
        else 
          house_info[[x]] <- ""
      }, b = hinf, bi = house_info)
  
  #Add labels, adds blank string if not relevant
  hinf.out <- lapply(c(1:length(group)), function(x, b, bi) {
      mutate(b[[x]], label = bi[[x]])}, b = hinf, bi = hi)
  names(hinf.out) <- group

	return(hinf.out)
}





#bldg_group <- house %>% group_by(hrrp_vcode, foundatn, roof) %>% filter(hrrp_vcode == "524 1 02 07 3 015")
# Produces sequential counts - i.e. counts both variables together
#wardbldgs <- bldg_group %>% summarize(count =  n())
bradleyswilson/hrrp documentation built on May 28, 2019, 7:13 p.m.