ksformat

PROC FORMAT for R GitHub License: GPL v3 R package

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).

Installation

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()

Features

Quick start

Discrete formatting

library(ksformat)

fnew(
  "M" = "Male",
  "F" = "Female",
  .missing = "Unknown",
  name = "sex"
)

fput(c("M", "F", NA, "X"), "sex")
# [1] "Male"    "Female"  "Unknown" "X"

Numeric ranges

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"

Reverse formatting (invalue)

finput("Male" = 1, "Female" = 2, name = "sex_inv")

finputn(c("Male", "Female", "Unknown"), "sex_inv")
# [1]  1  2 NA

Format library

fprint()              # list all registered formats
fmt <- format_get("sex")
fclear("sex")         # remove one format
fclear()              # clear all

Data frames

df <- 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")

Missing value handling

Priority order:

  1. NA, NULL, NaN.missing label if defined, otherwise NA
  2. Exact match → value–label mapping
  3. Range match → range label (numeric formats)
  4. No match.other label or original value

Options: keep_na = TRUE, na_if, include_empty = TRUE.

Cheat sheet

Function reference

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

Development

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.

License

GPL-3. See https://www.gnu.org/licenses/gpl-3.0.html.