diff --git a/NEWS.md b/NEWS.md index db6a356..6678872 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ implementations and in particularly relevant to data points with age exactly 24 months. Previously this was converted to 730 days and with this release it is converted to 731 days. +* The cleaned `measure` variable is now part of the output data.frame of + `anthro_zscores`. * Removed `covr` from suggests dependencies * Fixed a typo in the docs diff --git a/R/z-score.R b/R/z-score.R index 6995b75..3c372cf 100644 --- a/R/z-score.R +++ b/R/z-score.R @@ -218,8 +218,8 @@ anthro_zscores <- function(sex, sex <- standardize_sex_var(sex) # clean measure - measure <- tolower(trimws(measure)) - measure[!(is.na(measure) | measure %in% c("l", "h"))] <- NA_character_ + cmeasure <- tolower(trimws(measure)) + cmeasure[!(is.na(cmeasure) | cmeasure %in% c("l", "h"))] <- NA_character_ # clean oedema, we set all oedema not being "y" to "n" oedema <- standardize_oedema_var(oedema) @@ -229,25 +229,30 @@ anthro_zscores <- function(sex, # we consider a height measure for children younger than 9 months as # implausible - measure_implausible <- measure == "h" & age_in_months < 9 - measure[measure_implausible] <- NA_character_ + measure_implausible <- !is.na(cmeasure) & + !is.na(age_in_months) & + cmeasure == "h" & + age_in_months < 9 + cmeasure[measure_implausible] <- NA_character_ - clenhei <- adjust_lenhei(age_in_days, measure, lenhei) + clenhei <- adjust_lenhei(age_in_days, cmeasure, lenhei) cbmi <- weight / ((clenhei / 100)^2) cbind( clenhei, cbmi, + cmeasure, anthro_zscore_length_for_age(clenhei, age_in_days, sex), anthro_zscore_weight_for_age(weight, age_in_days, sex, oedema), anthro_zscore_weight_for_lenhei( - weight, clenhei, measure, age_in_days, + weight, clenhei, cmeasure, age_in_days, sex, oedema ), anthro_zscore_bmi_for_age(cbmi, age_in_days, sex, oedema), anthro_zscore_head_circumference_for_age(headc, age_in_days, sex), anthro_zscore_arm_circumference_for_age(armc, age_in_days, sex), anthro_zscore_triceps_skinfold_for_age(triskin, age_in_days, sex), - anthro_zscore_subscapular_skinfold_for_age(subskin, age_in_days, sex) + anthro_zscore_subscapular_skinfold_for_age(subskin, age_in_days, sex), + stringsAsFactors = FALSE ) } diff --git a/tests/testthat/test-zscores.R b/tests/testthat/test-zscores.R index 6802ae2..611aca2 100644 --- a/tests/testthat/test-zscores.R +++ b/tests/testthat/test-zscores.R @@ -187,6 +187,7 @@ test_that("young children with measured standing will not be adjusted", { ) expect_equal(res$clenhei, c(60, 60.7)) expect_equal(res$zlen, c(-4.81, -5.02), tolerance = 0.01) + expect_equal(res$cmeasure, c(NA_character_, "h")) }) test_that("zcores are only computed for children younger or equal to 60 months", { @@ -198,7 +199,7 @@ test_that("zcores are only computed for children younger or equal to 60 months", weight = 60, measure = "h" ) - expect_true(all(is.na(res[2, -1:-2]))) + expect_true(all(is.na(res[2, -1:-3]))) }) test_that("height measurements are used for age 24 months", {