Permalink
Browse files

Trim years outside range rather than error

Addresses #42
  • Loading branch information...
1 parent 0803ca1 commit ecd6bf71ba1eb123ffee598cb24b037b1ee2acd7 @lmullen lmullen committed Sep 10, 2016
Showing with 26 additions and 8 deletions.
  1. +16 −7 R/gender.R
  2. +1 −1 man/gender.Rd
  3. +9 −0 tests/testthat/test-argument-validation.r
View
@@ -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,15 +104,19 @@ 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.")
}
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,23 +130,28 @@ 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)
if ("United States" %in% countries)
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))
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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")
+})

0 comments on commit ecd6bf7

Please sign in to comment.