diff --git a/R/gender.R b/R/gender.R index f813d74..3be1d2a 100644 --- a/R/gender.R +++ b/R/gender.R @@ -25,7 +25,7 @@ #' U.S. Social Security Administration baby name data. (This method is based #' on an implementation by Cameron Blevins.) The \code{"ipums"} method looks #' up names from the U.S. Census data in the Integrated Public Use Microdata -#' Series. (This method was contributed by Ben Schmidt.) The \code{"napp"} +#' Series. (This method was contributed by Ben Schmidt.) The \code{"napp"} #' method uses census microdata from Canada, Great Britain, Germany, #' Iceland, Norway, and Sweden from 1801 to 1910 created by the #' \href{https://www.nappdata.org/napp/}{North Atlantic Population Project}. @@ -104,7 +104,9 @@ gender <- function(names, years = c(1932, 2012), # Hand off the arguments to functions based on method, and do error checking if (method == "ssa") { if (years[1] < 1880 || years[2] > 2012) { - stop("Please provide a year range between 1880 and 2012.") + warning("The year range provided has been trimmed to fit within 1880 to 2012.") + if (years[1] < 1880) years[1] <- 1880 + if (years[2] > 2012) years[2] <- 2012 } if (!missing(countries) && countries != "United States") { stop("SSA data is only available for the United States of America.") @@ -112,7 +114,9 @@ gender <- function(names, years = c(1932, 2012), gender_ssa(names = names, years = years) } else if (method == "demo") { if (years[1] < 1880 || years[2] > 2012) { - stop("Please provide a year range between 1880 and 2012.") + warning("The year range provided has been trimmed to fit within 1880 to 2012.") + if (years[1] < 1880) years[1] <- 1880 + if (years[2] > 2012) years[2] <- 2012 } if (!missing(countries) && countries != "United States") { stop("Demo data is only available for the United States of America.") @@ -126,15 +130,20 @@ gender <- function(names, years = c(1932, 2012), gender_kantrowitz(names = names) } else if (method == "ipums") { if (years[1] < 1789 || years[2] > 1930) { - stop("Please provide a year range between 1789 and 1930.") + warning("The year range provided has been trimmed to fit within 1789 to 1930.") + if (years[1] < 1789) years[1] <- 1789 + if (years[2] > 1930) years[2] <- 1930 } if (!missing(countries) && countries != "United States") { stop("IPUMS data is only available for the United States of America.") } gender_ipums_usa(names = names, years = years) } else if (method == "napp") { - if (years[1] < 1758 || years[2] > 1910) - stop("Please provide a year range between 1758 and 1910.") + if (years[1] < 1758 || years[2] > 1910) { + warning("The year range provided has been trimmed to fit within 1758 to 1910.") + if (years[1] < 1758) years[1] <- 1758 + if (years[2] > 1910) years[2] <- 1910 + } if (missing(countries)) countries <- countries[countries != "United States"] countries <- match.arg(countries, several.ok = TRUE) @@ -142,7 +151,7 @@ gender <- function(names, years = c(1932, 2012), stop("NAPP data is only available for European countries. See ", "the documentation.") gender_napp(names = names, years = years, countries = countries) - }else if (method == "genderize") { + } else if (method == "genderize") { if (!missing(years)) stop("Genderize method does not account for year.") if (!missing(countries)) diff --git a/man/gender.Rd b/man/gender.Rd index a7405d6..e56c371 100644 --- a/man/gender.Rd +++ b/man/gender.Rd @@ -27,7 +27,7 @@ gender of the name. The \code{"ssa"} method looks up names based from the U.S. Social Security Administration baby name data. (This method is based on an implementation by Cameron Blevins.) The \code{"ipums"} method looks up names from the U.S. Census data in the Integrated Public Use Microdata -Series. (This method was contributed by Ben Schmidt.) The \code{"napp"} +Series. (This method was contributed by Ben Schmidt.) The \code{"napp"} method uses census microdata from Canada, Great Britain, Germany, Iceland, Norway, and Sweden from 1801 to 1910 created by the \href{https://www.nappdata.org/napp/}{North Atlantic Population Project}. diff --git a/tests/testthat/test-argument-validation.r b/tests/testthat/test-argument-validation.r index 0be4576..4b9365b 100644 --- a/tests/testthat/test-argument-validation.r +++ b/tests/testthat/test-argument-validation.r @@ -42,3 +42,12 @@ test_that("countries are mapped with their respective methods", { "NAPP data is only available for European countries.") expect_error(gender("Madison", method = "napp", countries = "New South Wales")) }) + +test_that("year ranges out of scope of data are trimmed", { + expect_warning(gender("Jason", method = "ssa", years = c(1860, 1950)), + "The year range provided has been trimmed") + expect_warning(gender("Jason", method = "ipums", years = c(1700, 1950)), + "The year range provided has been trimmed") + expect_warning(gender("Jason", method = "napp", years = c(1754, 1765)), + "The year range provided has been trimmed") +})