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..5dcb9ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +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 diff --git a/R/obfuscate_gps.R b/R/obfuscate_gps.R index 16a30a8..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,11 @@ 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,...) @@ -122,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") } @@ -130,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) } @@ -154,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 ") } @@ -162,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 @@ -199,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),