Skip to content

Commit

Permalink
feat: changed function names to prevent any conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Oct 10, 2021
1 parent 5e4a123 commit 4c0e4ca
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 96 deletions.
10 changes: 5 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(abbr)
export(county)
export(geometry)
export(metadata)
export(state)
export(fips_abbr)
export(fips_county)
export(fips_geometry)
export(fips_metadata)
export(fips_state)
10 changes: 5 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

* Added a `NEWS.md` file to track changes to the package.
* Initial commit to version control with the following functions:
- `abbr()` - Gets state abbreviation.
- `state()` - Gets state name.
- `county()` - Gets county name.
- `geometry()` - Gets geometry.
- `metadata()` - Gets the information above as a `data.frame`.
- `fips_abbr()` - Gets state abbreviation.
- `fips_state()` - Gets state name.
- `fips_county()` - Gets county name.
- `fips_geometry()` - Gets geometry.
- `fips_metadata()` - Gets the information above as a `data.frame`.
42 changes: 21 additions & 21 deletions R/fipio.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@
#' @param fip 2-digit or 5-digit FIPS code
#' @return a `character` vector
#' @examples
#' fipio::abbr("37")
#' fipio::abbr("06001")
#' fipio::fips_abbr("37")
#' fipio::fips_abbr("06001")
#'
#' @export
abbr <- function(fip) {
fips_abbr <- function(fip) {
tmp <- unique(fips_[, c(1, 4)])
tmp[[2]][match(substr(fip, 1, 2), tmp[[1]])]
}

#' @title Get the state name for a FIPS code
#' @inheritParams abbr
#' @inheritParams fips_abbr
#' @return a `character` vector
#' @examples
#' fipio::state("37")
#' fipio::state("06001")
#' fipio::fips_state("37")
#' fipio::fips_state("06001")
#'
#' @export
state <- function(fip) {
fips_state <- function(fip) {
tmp <- unique(fips_[, c(1, 5)])
tmp[[2]][match(substr(fip, 1, 2), tmp[[1]])]
}

#' @title Get the county name for a FIPS code
#' @inheritParams abbr
#' @inheritParams fips_abbr
#' @return a `character` vector
#' @examples
#' fipio::county("37129")
#' fipio::county("06001")
#' fipio::fips_county("37129")
#' fipio::fips_county("06001")
#'
#' # 2-digit FIP codes will not work
#' fipio::county("37")
#' fipio::fips_county("37")
#'
#' @export
county <- function(fip) {
fips_county <- function(fip) {
tmp <- fips_[, c(3, 6)]
tmp[[2]][match(fip, tmp[[1]])]
}


#' @title Get the geometry for a FIPS code
#' @inheritParams abbr
#' @inheritParams fips_abbr
#' @return an `sfg`/`sfc` object
#' @examples
#' \dontrun{
#' fipio::geometry("37")
#' fipio::geometry("06001")
#' fipio::fips_geometry("37")
#' fipio::fips_geometry("06001")
#' }
#'
#' @export
geometry <- function(fip) {
fips_geometry <- function(fip) {
if (.has_sfheaders()) {
geo_$geometry[match(fip, geo_$fip_code)]
} else {
Expand All @@ -60,15 +60,15 @@ geometry <- function(fip) {
}

#' @title Get the metadata for a FIPS code
#' @inheritParams abbr
#' @inheritParams fips_abbr
#' @param geometry If `TRUE`, returns a geometry column (requires `sfheaders`)
#' @return a `data.frame`
#' @examples
#' fipio::metadata("37")
#' fipio::metadata("06001")
#' fipio::fips_metadata("37")
#' fipio::fips_metadata("06001")
#'
#' @export
metadata <- function(fip, geometry = FALSE) {
fips_metadata <- function(fip, geometry = FALSE) {
df <- do.call(rbind, lapply(
X = fip,
FUN = function(f) {
Expand All @@ -89,7 +89,7 @@ metadata <- function(fip, geometry = FALSE) {
}
))

if (geometry) df$geometry <- geometry(df$fip_code)
if (geometry) df$geometry <- fips_geometry(df$fip_code)

rownames(df) <- NULL

Expand Down
26 changes: 13 additions & 13 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ Let's answer a few questions that might come up if you have a FIPS code:
fip <- "37129"
# What state is `37129` in?
fipio::state(fip)
fipio::fips_state(fip)
# Alternatively, you can use the state FIPS code by itself
fipio::state("37")
fipio::fips_state("37")
# What about the state abbreviation?
fipio::abbr(fip)
fipio::fips_abbr(fip)
# What county is `37129`?
fipio::county(fip)
fipio::fips_county(fip)
# It'd be nice to have this all in a data.frame...
fipio::metadata(fip)
fipio::fips_metadata(fip)
# And the metadata for the state by itself...
fipio::metadata("37")
fipio::fips_metadata("37")
```

### With `sf`
Expand All @@ -79,24 +79,24 @@ library(sf, quietly = TRUE)

```{r}
# I'm doing spatial work, what's the geometry of `37129`?
fipio::geometry(fip)
fipio::fips_geometry(fip)
# What if I need it with my other metadata?
fipio::metadata(fip, geometry = TRUE)
fipio::fips_metadata(fip, geometry = TRUE)
```

### Vectorized
`fipio` functions are inherently vectorized, so you can use them with vectors of FIPS codes easily:
```{r}
fips <- c("37129", "44001", "48115")
fipio::state(fips)
fipio::fips_state(fips)
fipio::abbr(fips)
fipio::fips_abbr(fips)
fipio::county(fips)
fipio::fips_county(fips)
fipio::metadata(fips)
fipio::fips_metadata(fips)
fipio::geometry(fips)
fipio::fips_geometry(fips)
```
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@ answer a few questions that might come up if you have a FIPS code:
fip <- "37129"

# What state is `37129` in?
fipio::state(fip)
fipio::fips_state(fip)
#> [1] "North Carolina"

# Alternatively, you can use the state FIPS code by itself
fipio::state("37")
fipio::fips_state("37")
#> [1] "North Carolina"

# What about the state abbreviation?
fipio::abbr(fip)
fipio::fips_abbr(fip)
#> [1] "NC"

# What county is `37129`?
fipio::county(fip)
fipio::fips_county(fip)
#> [1] "New Hanover"

# It'd be nice to have this all in a data.frame...
fipio::metadata(fip)
fipio::fips_metadata(fip)
#> state_code county_code fip_code state_abbr state_name county_name
#> 1 37 129 37129 NC North Carolina New Hanover

# And the metadata for the state by itself...
fipio::metadata("37")
fipio::fips_metadata("37")
#> state_code state_abbr state_name fip_code
#> 1 37 NC North Carolina 37
```
Expand All @@ -80,7 +80,7 @@ geometry object back.

``` r
# I'm doing spatial work, what's the geometry of `37129`?
fipio::geometry(fip)
fipio::fips_geometry(fip)
#> Geometry set for 1 feature
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
Expand All @@ -89,7 +89,7 @@ fipio::geometry(fip)
#> MULTIPOLYGON (((-78.02992 34.33177, -77.82268 3...

# What if I need it with my other metadata?
fipio::metadata(fip, geometry = TRUE)
fipio::fips_metadata(fip, geometry = TRUE)
#> state_code county_code fip_code state_abbr state_name county_name
#> 1 37 129 37129 NC North Carolina New Hanover
#> geometry
Expand All @@ -104,22 +104,22 @@ vectors of FIPS codes easily:
``` r
fips <- c("37129", "44001", "48115")

fipio::state(fips)
fipio::fips_state(fips)
#> [1] "North Carolina" "Rhode Island" "Texas"

fipio::abbr(fips)
fipio::fips_abbr(fips)
#> [1] "NC" "RI" "TX"

fipio::county(fips)
fipio::fips_county(fips)
#> [1] "New Hanover" "Bristol" "Dawson"

fipio::metadata(fips)
fipio::fips_metadata(fips)
#> state_code county_code fip_code state_abbr state_name county_name
#> 1 37 129 37129 NC North Carolina New Hanover
#> 2 44 001 44001 RI Rhode Island Bristol
#> 3 48 115 48115 TX Texas Dawson

fipio::geometry(fips)
fipio::fips_geometry(fips)
#> Geometry set for 3 features
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
Expand Down
10 changes: 5 additions & 5 deletions man/abbr.Rd → man/fips_abbr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/county.Rd → man/fips_county.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/geometry.Rd → man/fips_geometry.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/metadata.Rd → man/fips_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/state.Rd → man/fips_state.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4c0e4ca

Please sign in to comment.