Skip to content

Commit

Permalink
Add lookup().
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolepore committed May 14, 2018
1 parent 9e51b65 commit e3c577c
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 12 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export(fieldforms_output)
export(fieldforms_prepare)
export(fill_na)
export(hide_data_of_class)
export(lookup)
export(ls_csv_df)
export(ls_join_df)
export(ls_list_spreadsheets)
Expand Down
43 changes: 43 additions & 0 deletions R/lookup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' `recode()` by looking up `old` and `new` values (from a lookup table).
#'
#' Use this function inside `dplyr::recode()` to recode a vector based on values
#' from two other vectors, where `old` and `new` codes are looked up. These
#' lookup vectors are commonly stored in a dataframe and come from a .csv or
#' spreadsheet file.
#'
#' @param old,new Vectors of equal length giving old and new codes.
#'
#' @seealso `dplyr::recode()`
#'
#' @return A "spliced" list with names from `old` and values from `new`. The
#' kind of data structure that you can feed to `...` in dplyr::recode()`.
#'
#' @export
#'
#' @examples
#' set.seed(1)
#' library(dplyr, warn.conflicts = FALSE)
#' library(rlang)
#'
#' look <- tibble(
#' old = c("spp1", "unknown"),
#' new = c("spp3", "spp4")
#' )
#'
#' lookup(look$old, look$new)
#'
#' x <- c("spp1", "spp2", "spp3", "unknown", "spp3", "unknown", "spp1", "spp1")
#' x
#' recode(x, lookup(look$old, look$new))
#' # Same
#' recode(x, !!!as.list(set_names(look$new, look$old)))
#'
#' dfm <- tibble(x = x)
#' mutate(dfm, new_x = recode(x, lookup(look$old, look$new)))
lookup <- function(old, new) {
if (length(old) != length(new)) {
stop("`old` and `new` must be of equal length.")
}
lookup <- rlang::set_names(new, old)
rlang::splice(as.list(lookup))
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ reference:

- title: Manipulate dataframes
contents:
- lookup
- recode_subquad

- title: Manipulate strings
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/read_df_write.html

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

10 changes: 8 additions & 2 deletions docs/reference/count_duplicated.html

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

8 changes: 4 additions & 4 deletions docs/reference/fieldforms_output.html

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

6 changes: 6 additions & 0 deletions docs/reference/index.html

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

228 changes: 228 additions & 0 deletions docs/reference/lookup.html

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

Loading

0 comments on commit e3c577c

Please sign in to comment.