SAS-style PROC FORMAT for R: create and apply value formats, range-based formatting, reverse formatting (invalue), and consistent handling of missing values (NA, NULL, NaN).
From GitHub (after cloning or from your repo URL):
# install.packages("remotes")
remotes::install_github("crow16384/ksformat")From local source:
install.packages(".", repos = NULL, type = "source")
# or
devtools::install()library(ksformat)
fnew(
"M" = "Male",
"F" = "Female",
.missing = "Unknown",
name = "sex"
)
fput(c("M", "F", NA, "X"), "sex")
# [1] "Male" "Female" "Unknown" "X"fparse(text = '
VALUE age (numeric)
[0, 18) = "Child"
[18, 65) = "Adult"
[65, HIGH] = "Senior"
.missing = "Age Unknown"
;
')
fputn(c(5, 25, 70, NA), "age")
# [1] "Child" "Adult" "Senior" "Age Unknown"finput("Male" = 1, "Female" = 2, name = "sex_inv")
finputn(c("Male", "Female", "Unknown"), "sex_inv")
# [1] 1 2 NAfprint() # list all registered formats
fmt <- format_get("sex")
fclear("sex") # remove one format
fclear() # clear alldf <- data.frame(
sex = c("M", "F", "M", NA),
age = c(15, 25, 70, 35)
)
fput_df(df, sex = format_get("sex"), age = format_get("age"), suffix = "_label")Priority order:
.missing label if
defined, otherwise NA.other label or original
valueOptions: keep_na = TRUE, na_if,
include_empty = TRUE.
ksformat_cheatsheet() to
open the cheat sheet in your browser (HTML), or
ksformat_cheatsheet("pdf") for the PDF.| Area | Functions |
|---|---|
| Creation | fnew(), finput(), fnew_bid(),
fnew_date(), fparse() |
| Application | fput(), fputn(), fputc(),
fput_all(), fput_df() |
| Reverse | finputn(), finputc() |
| Library | format_get(), fprint(),
fclear(), fexport(),
fimport() |
| Utilities | is_missing(), range_spec() |
| Documentation | ksformat_cheatsheet() — open cheat sheet |
install.packages(c("roxygen2", "testthat", "devtools"))
devtools::document()
devtools::test()
devtools::check()When bumping the package version, update DESCRIPTION and
then run
Rscript scripts/sync-version.R to refresh version
references in cran-comments.md and any other synced
files.
GPL-3. See https://www.gnu.org/licenses/gpl-3.0.html.