knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
rcoder outlines a lightweight data structure that captures categorical codings and easily converts them to other implementations.
rcoder's main functions are coding()
and code()
. code()
maps a character-valued key to some value to represent a categorical level.
library(rcoder) library(magrittr) code("Yes", 1)
code()
objects can hold an arbitrary amount of metadata. Useful pieces of information to know are descriptions of what levels represent (in case the label doesn't have enough information) and whether or not the value represents a missing value.
code( "No response", -99, description = "Surveyed individual ignored question when asked", missing = TRUE )
coding()
is a collection of code()
objects that represents the full set of values for a categorical variable.
coding( code("Don't know", 0, missing = TRUE), code("Never", 1), code("Rarely", 2), code("Sometimes", 3), code("Frequently", 4), code("Always", 5), code("No response", -99, missing = TRUE), code("Refused", -88, missing = TRUE), code("Absent", -77, missing = TRUE) )
coding()
objects are designed to be an intermediate representation of categorical data so that they can be converted into different representations on the fly, e.g. ODK XLSForm choices and STATA/SPSS columns via haven
.
coding( code("Never", 1), code("Rarely", 2), code("Sometimes", 3), code("Frequently", 4), code("Always", 5), .label = "frequency" ) %>% coding_to_odk()
To facilitate recoding, coding()
objects link to one another through the code()
labels. If multiple values are collapsed into one, use the links_from
parameter to identify which values are combined into one.
original_coding <- coding( code("No", 0L), code("Yes", 1L), code("No response", -99L, missing = TRUE), code("Refused", -88L, missing = TRUE), code("Absent", -77L, missing = TRUE) ) new_coding <- coding( code("No", 0L), code("Yes", 1L), code("Missing", NA, links_from = c("No response", "Refused", "Absent")) ) new_coding %>% link_codings(original_coding)
These linked codings can be converted into a function that accepts a vector and returns a recoded vector.
new_coding %>% link_codings(original_coding) %>% make_recode_query()
Install rcoder from CRAN with:
install.packages("rcoder")
To get the latest stable changes that may not be on CRAN, install from the nyuglobalties r-universe with:
install.packages("rcoder", repos = "https://nyuglobalties.r-universe.dev")
Finally, get the development version straight from GitHub:
# install.packages("remotes") remotes::install_github("nyuglobalties/rcoder")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.