Commit 8a4c511e authored by Laura A DeCicco's avatar Laura A DeCicco
Browse files

Fixes #564

parent 5db2c89e
......@@ -276,21 +276,22 @@ countyCdLookup <- function(state, county, outputType = "id"){
stop("No county code provided")
}
if(length(state) > 1){
stop("Only one state allowed in countyCdLookup.")
}
#first turn state into stateCd postal name
stateCd <- stateCdLookup(state,outputType = "postal")
state_counties <- countyCd[countyCd$STUSAB == stateCd,]
if(is.numeric(county) | !is.na(suppressWarnings(as.numeric(county)))){
county_i <- which(as.numeric(county) == as.numeric(countyCd$COUNTY) & stateCd == countyCd$STUSAB)
} else {
# if no suffix was added, this will figure out what it should be (or throw a helpful error)
allSuffixes <- unique(tolower(unlist(lapply(strsplit(countyCd$COUNTY_NAME,split=" "), tail, 1))))
county_in_state <- grep(tolower(county), tolower(state_counties$COUNTY_NAME))
county_i <- unlist(lapply(allSuffixes, function(suffix, stateCd, county){
currentSuffixExistsInString <- grepl(paste0('(?i)\\', suffix, '$'), tolower(county))
retCounty <- ifelse(currentSuffixExistsInString, county, paste(county, suffix))
retCounty_i <- which(tolower(retCounty) == tolower(countyCd$COUNTY_NAME) & stateCd == countyCd$STUSAB)
return(retCounty_i)
}, stateCd, county))
county_i <- which(countyCd$STUSAB == stateCd &
countyCd$COUNTY_NAME == state_counties$COUNTY_NAME[county_in_state] )
if(length(county_i) == 0){
stop(paste("Could not find", county, "(county),", stateCd,
......
......@@ -129,9 +129,9 @@
#' querySummary=TRUE)
#'
#' # querying by county
#' dailyLexingtonVA <- readWQPdata(statecode = "Virginia",
#' countycode="Lexington",
#' parameterCd = "00010")
#' DeWitt <- readWQPdata(statecode = "Illinois",
#' countycode= "DeWitt",
#' characteristicName = "Nitrogen")
#'
#' # Data profiles: "Organization Data"
#' org_data <- readWQPdata(statecode = "WI",
......
No preview for this file type
......@@ -86,8 +86,9 @@ NULL
#' US State Code Lookup Table
#'
#' Data pulled from \url{https://www2.census.gov/geo/docs/reference/state.txt}
#' on April 1, 2015.
#' Data originally pulled from \url{https://www2.census.gov/geo/docs/reference/state.txt}
#' on April 1, 2015. On Feb. 11, 2022, the fields were updated with the
#' file found in inst/extdata, which is used internally with NWIS retrievals.
#'
#' @name stateCd
#' @return stateCd data frame.
......@@ -108,8 +109,9 @@ NULL
#' US County Code Lookup Table
#'
#' Data pulled from \url{https://www2.census.gov/geo/docs/reference/codes/files/national_county.txt}
#' on April 1, 2015.
#' Data originally pulled from \url{https://www2.census.gov/geo/docs/reference/codes/files/national_county.txt}
#' on April 1, 2015. On Feb. 11, 2022, the fields were updated with the
#' file found in inst/extdata, which is used internally with NWIS retrievals.
#'
#' @name countyCd
#' @return countyCd data frame.
......@@ -127,4 +129,38 @@ NULL
#' @keywords USGS countyCd
#' @examples
#' head(countyCd)
NULL
\ No newline at end of file
NULL
# x <- jsonlite::read_json("inst/extdata/state_county.json")
# states <- x[["US"]]
# state_df <- data.frame("STATE" = names(sapply(states$state_cd, function(x) x[[1]])),
# "STATE_NAME" = as.character(sapply(states$state_cd, function(x) x[[1]])))
# library(dplyr)
# state_df <- state_df %>%
# left_join(stateCd, by = c("STATE", "STATE_NAME"))
# state_df$STUSAB[state_df$STATE_NAME == "Virgin Islands"] <- "VI"
#
# y <- sapply(states$state_cd, function(x) x[["county_cd"]])
#
# county_df <- data.frame()
#
# for(st in state_df$STATE){
# county_nums <- names(y[[st]])
# county_names <- as.character(unlist(y[[st]]))
# county_df_st <- data.frame(STATE = st,
# COUNTY = county_nums,
# COUNTY_NAME = county_names)
# county_df <- dplyr::bind_rows(county_df, county_df_st)
# }
#
# county_df_full <- county_df %>%
# left_join(select(state_df, STUSAB, STATE), by = "STATE") %>%
# left_join(select(countyCd, STATE, STUSAB, COUNTY, COUNTY_ID),
# by = c("STATE", "STUSAB", "COUNTY"))
#
# countyCd <- county_df_full
# stateCd <- state_df
# save(countyCd, stateCd, parameterCdFile, pCodeToName,
# file = "sysdata.rda", compress = "xz")
This diff is collapsed.
......@@ -17,8 +17,9 @@ COUNTY_ID \tab character \tab County id \cr
}
}
\description{
Data pulled from \url{https://www2.census.gov/geo/docs/reference/codes/files/national_county.txt}
on April 1, 2015.
Data originally pulled from \url{https://www2.census.gov/geo/docs/reference/codes/files/national_county.txt}
on April 1, 2015. On Feb. 11, 2022, the fields were updated with the
file found in inst/extdata, which is used internally with NWIS retrievals.
}
\examples{
head(countyCd)
......
......@@ -142,9 +142,9 @@ wqp.summary_WI <- readWQPdata(arg_3, statecode="WI",
querySummary=TRUE)
# querying by county
dailyLexingtonVA <- readWQPdata(statecode = "Virginia",
countycode="Lexington",
parameterCd = "00010")
DeWitt <- readWQPdata(statecode = "Illinois",
countycode= "DeWitt",
characteristicName = "Nitrogen")
# Data profiles: "Organization Data"
org_data <- readWQPdata(statecode = "WI",
......
......@@ -16,8 +16,9 @@ STATENS \tab character \tab Geographic Names Information System Identifier (GNI
}
}
\description{
Data pulled from \url{https://www2.census.gov/geo/docs/reference/state.txt}
on April 1, 2015.
Data originally pulled from \url{https://www2.census.gov/geo/docs/reference/state.txt}
on April 1, 2015. On Feb. 11, 2022, the fields were updated with the
file found in inst/extdata, which is used internally with NWIS retrievals.
}
\examples{
head(stateCd)
......
......@@ -194,7 +194,7 @@ test_that("state county tests",{
name <- countyCdLookup(state = "OH", county = 13, output = "fullName")
expect_equal(name, "Belmont County")
index <- countyCdLookup(state = "Pennsylvania", county = "ALLEGHENY COUNTY", output = "tableIndex")
expect_equal(index, 2246)
expect_equal(index, 2258)
fromIDs <- countyCdLookup(state = 13, county = 5, output = "fullName")
expect_equal(fromIDs, "Bacon County")
counties <- c("51001","51003")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment