From 1be11d3eccc9620b9701245ec9aa0545a5451e2f Mon Sep 17 00:00:00 2001 From: Collin Schwantes Date: Wed, 11 Sep 2024 14:49:07 -0600 Subject: [PATCH 1/4] adding check for numeric --- R/obfuscate_gps.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/obfuscate_gps.R b/R/obfuscate_gps.R index 16a30a8..e04dff0 100644 --- a/R/obfuscate_gps.R +++ b/R/obfuscate_gps.R @@ -62,6 +62,10 @@ obfuscate_gps <- function(x, precision = 2, fuzz = 0.125, type = c("lat","lon"), func = min, ...){ + if(!is.numeric(x)){ + stop("x must be numeric") + } + ## max precision in your data # find value in x with most decimal points data_precision <- get_precision(x,func = func,...) From 21e0c44f1a80563f75d34fb50215ad0e6157f44c Mon Sep 17 00:00:00 2001 From: Collin Schwantes Date: Thu, 12 Sep 2024 17:24:14 -0600 Subject: [PATCH 2/4] Increment version number to 0.3.11 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 15da133..6849728 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ohcleandat Type: Package Title: One Health Data Cleaning and Quality Checking Package -Version: 0.3.10 +Version: 0.3.11 Authors@R: c( person("Collin", "Schwantes", email = "schwantes@ecohealthalliance.org", role = c("cre", "aut"), comment = c(ORCID = "0000-0003-4014-4896")), person("Johana", "Teigen", email = "teigen@ecohealthalliance.org", role = "aut", comment = c(ORCID = "0000-0002-6209-2321")), diff --git a/NEWS.md b/NEWS.md index dbc6f8a..4173fd8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# ohcleandat 0.3.11 + # ohcleandat 0.3.10 * datapackage.json can be pruned to more closely follow structural metadata for From e8b6f9f21a6f6ba1920d9b9c0a0c1c1595c4bfd8 Mon Sep 17 00:00:00 2001 From: Collin Schwantes Date: Thu, 12 Sep 2024 17:47:26 -0600 Subject: [PATCH 3/4] elegently handle gps points --- R/obfuscate_gps.R | 23 ++++++++++++++++++++--- man/obfuscate_gps.Rd | 6 ++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/R/obfuscate_gps.R b/R/obfuscate_gps.R index e04dff0..9193456 100644 --- a/R/obfuscate_gps.R +++ b/R/obfuscate_gps.R @@ -48,6 +48,12 @@ #' gps_data_180$lon |> #' obfuscate_gps(fuzz = 1, type = "lon") #' +#' ### working NA GPS data +#' gps_data_180 <- data.frame(lat = c(2, 3, 4), +#' lon = c(179.39595, NA, -178.09999901)) +#' gps_data_180$lon |> +#' obfuscate_gps(fuzz = 1, type = "lon") +#' #' ### GPS is on the fritz! #' \dontrun{ #' gps_data_fritz <- data.frame(lat = c(91, -91, 90), @@ -62,6 +68,7 @@ obfuscate_gps <- function(x, precision = 2, fuzz = 0.125, type = c("lat","lon"), func = min, ...){ + if(!is.numeric(x)){ stop("x must be numeric") } @@ -126,7 +133,9 @@ obfuscate_lat <- function(x, precision = 2, fuzz = 0.125){ stop("fuzz greater than range of latitude on earth") } - if(any(x > 90 | x < -90)){ + range_check <- stats::na.omit(x > 90 | x < -90) + + if(any(range_check)){ stop("Latitude is outside the range of latitude on earth") } @@ -134,6 +143,9 @@ obfuscate_lat <- function(x, precision = 2, fuzz = 0.125){ # make sure point is between 90 and -90 points_in_range <- purrr::map_dbl(points,function(point){ + if(is.na(point)){ + return(point) + } while(all(point > 90 | point < -90)){ point <- obfuscate_point(point,precision,fuzz) } @@ -158,7 +170,8 @@ obfuscate_lon <- function(x, precision = 2, fuzz = 0.125){ stop("fuzz greater than range of longitude on earth") } - if(any(x > 180 | x < -180)){ + range_check <- stats::na.omit(x > 180 | x < -180) + if(any(range_check)){ stop("Longitude is outside the range of longitude on earth ") } @@ -166,7 +179,9 @@ obfuscate_lon <- function(x, precision = 2, fuzz = 0.125){ ### wrap points near the 180th meridian points_in_range <- purrr::map_dbl(points,function(point){ - + if(is.na(point)){ + return(point) + } # if point greater than 180, wrap if(point > 180){ difference <- point - 180 @@ -203,6 +218,8 @@ obfuscate_lon <- function(x, precision = 2, fuzz = 0.125){ #' #' get_precision <- function(x,func = c,...) { + # drop NAs + x <- stats::na.omit(x) # number of characters with the decimal - number of characters without it precision <- 10^-(nchar(gsub("\\.", "", as.character(x))) - nchar(as.character(trunc(x)))) out <- func(precision,...) diff --git a/man/obfuscate_gps.Rd b/man/obfuscate_gps.Rd index bbc4162..46f46fa 100644 --- a/man/obfuscate_gps.Rd +++ b/man/obfuscate_gps.Rd @@ -77,6 +77,12 @@ gps_data_180 <- data.frame(lat = c(2, 3, 4), gps_data_180$lon |> obfuscate_gps(fuzz = 1, type = "lon") +### working NA GPS data +gps_data_180 <- data.frame(lat = c(2, 3, 4), + lon = c(179.39595, NA, -178.09999901)) +gps_data_180$lon |> + obfuscate_gps(fuzz = 1, type = "lon") + ### GPS is on the fritz! \dontrun{ gps_data_fritz <- data.frame(lat = c(91, -91, 90), From fe0db3166c39d89d2ca808a337fdb94715bdd32f Mon Sep 17 00:00:00 2001 From: Collin Schwantes Date: Thu, 12 Sep 2024 17:47:56 -0600 Subject: [PATCH 4/4] news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4173fd8..5dcb9ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ohcleandat 0.3.11 +* obfuscate gps can now handle NAs + # ohcleandat 0.3.10 * datapackage.json can be pruned to more closely follow structural metadata for