From d8db387afca91510914aa3b1fdd2ec19129b1941 Mon Sep 17 00:00:00 2001 From: James Clark Date: Fri, 9 Feb 2024 15:20:14 -0700 Subject: [PATCH 1/2] make missing weights 0 --- DESCRIPTION | 2 +- R/hts_prep_byvar.R | 22 +++++++++++++++++++++- R/hts_prep_data.R | 18 +++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index bd6b6fd..b4b2ba4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: travelSurveyTools Title: travelSurveyTools -Version: 2.0.0 +Version: 2.0.1 Authors@R: c( person("RSG", "Inc.", , "rsg@rsginc.com", role = c("aut", "cre")), person("Ashley", "Asmus", , "ashley.asmus@rsginc.com", role = "aut"), diff --git a/R/hts_prep_byvar.R b/R/hts_prep_byvar.R index a490552..4fec867 100644 --- a/R/hts_prep_byvar.R +++ b/R/hts_prep_byvar.R @@ -47,6 +47,8 @@ hts_prep_byvar = function(summarize_by = NULL, tbl_idx = which(names(hts_data) == byvar_loc) + wtname = byvar_wts[tbl_idx] + byvar_dt_v = data.table::copy(hts_data[[byvar_loc]]) # Check that specified id column exists in var_dt @@ -59,6 +61,24 @@ hts_prep_byvar = function(summarize_by = NULL, # Is this a numeric variable? byvar_is_numeric = variables_dt[shared_name == summarize_by[[b]], data_type][[1]] == "numeric" + #check for missing weight variables + missing_weight_count = byvar_dt_v[is.na(get(wtname)), .N] + + if (missing_weight_count > 0){ + + message(missing_weight_count, ' missing observation(s) of ', wtname, + ' in ', byvar_loc, ' setting equal to 0') + + setnames(byvar_dt_v, wtname, 'old_weight') + + byvar_dt_v[, old_weight := ifelse(is.na(old_weight), + 0, + old_weight)] + + setnames(byvar_dt_v, 'old_weight', wtname) + + } + if (byvar_is_shared) { byvar_dt_v = @@ -113,4 +133,4 @@ hts_prep_byvar = function(summarize_by = NULL, ## quiets concerns of R CMD check -utils::globalVariables(c("variables_list", "is_checkbox", "data_type")) +utils::globalVariables(c("variables_list", "is_checkbox", "data_type", "old_weight")) diff --git a/R/hts_prep_data.R b/R/hts_prep_data.R index 9aa141a..f4eb368 100644 --- a/R/hts_prep_data.R +++ b/R/hts_prep_data.R @@ -127,18 +127,27 @@ hts_prep_data = function(summarize_var = NULL, tbl_idx = which(names(data) == var_location) + wtname = wt_cols[tbl_idx] + # Select table where this variable lives: var_dt = data[[var_location]] - browser() #check for missing weight variables - missing_weight_count = var_dt[is.na(ids[tbl_idx]), .N] + missing_weight_count = var_dt[is.na(get(wtname)), .N] if (missing_weight_count > 0){ - message(missing_weight_count, ' missing weight(s) of ', ids[tbl_idx], + message(missing_weight_count, ' missing observation(s) of ', wtname, ' in ', var_location, ' setting equal to 0') + setnames(var_dt, wtname, 'old_weight') + + var_dt[, old_weight := ifelse(is.na(old_weight), + 0, + old_weight)] + + setnames(var_dt, 'old_weight', wtname) + } # Check that specified id column exists in var_dt @@ -172,7 +181,6 @@ hts_prep_data = function(summarize_var = NULL, sum_vars_id_cols = intersect(id_cols, names(var_dt)) # Subset table to these column(s): - wtname = wt_cols[tbl_idx] if (weighted){ @@ -386,4 +394,4 @@ hts_prep_data = function(summarize_var = NULL, ## quiets concerns of R CMD check -utils::globalVariables(c("hts_data", "is_checkbox", "data_type")) +utils::globalVariables(c("hts_data", "is_checkbox", "data_type", "old_weight")) From 33ae360c66961bb3961aa4f98a164caa29a45c4c Mon Sep 17 00:00:00 2001 From: James Clark Date: Mon, 12 Feb 2024 14:30:29 -0700 Subject: [PATCH 2/2] remove white space from description --- DESCRIPTION | 2 -- 1 file changed, 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c09c3f6..84f56ab 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,6 @@ Package: travelSurveyTools Title: travelSurveyTools - Version: 2.3.0 - Authors@R: c( person("RSG", "Inc.", , "rsg@rsginc.com", role = c("aut", "cre")), person("Ashley", "Asmus", , "ashley.asmus@rsginc.com", role = "aut"),