R/C_hss_grow_dic.R

Defines functions C_hss_grow_dic

Documented in C_hss_grow_dic

#' Update dictionary to include new variables
#'
#' This function takes the input produced by C_hss_add_var to enlarge the original
#' dictionary to include any variable generated during the cleaning/analysis process.
#'
#' @param dic a dictionary object that is generated using the survey XLS form
#'
#' @return a modified dictionary object which includes the new variable generated
#' C_hss_add_var
#' @export
#'
#' @rdname C_hss_grow_dic

C_hss_grow_dic <- function(dic){

  #load modified doc generated by C_hss_add_var
  new_var <- readxl::read_xlsx("new_var.xls")

  #create an empty variable space to add to dictionary
  update <- matrix(nrow = 1, ncol = ncol(dic$val))
  colnames(update) <- colnames(dic$val)
  update <- as.data.frame(update)

  sample <- subset(dic[[2]], list_name == as.character(new_var$variable_name))
  update[1,] <- sample[1,]

  #fill out the key details
  update$name[1] <- new_var$value_number
  update$r_name[1] <- new_var$r_name
  update$label_english <- new_var$english_label
  update$label_arabic <- new_var$arabic_label
  update$label_stata <- new_var$english_label
  update$r_table_label_ar <- new_var$arabic_label
  update$r_table_label_en <- new_var$english_label

  #Reconstruct the dictionary
  val_col <- dic$val
  var_col <- dic$var
  new_val <- rbind(val_col[1:new_var$limit, ], update, val_col[- (1:new_var$limit), ])

  dic1 <- list(new_val)
  dic2 <- list(var_col)
  dic_updated <- c(dic2, dic1)

  names(dic_updated) <- c("var", "val")

  return(dic_updated)
}
RenRMT/hsstools documentation built on April 14, 2025, 7:10 p.m.