box_it: Convert ordinal variables into binary variables by "boxing"

View source: R/box_it.R

box_itR Documentation

Convert ordinal variables into binary variables by "boxing"

Description

For instance, you can create a Top Two Box variable from a 7-point agreement scale question. Function returns a labelled double binary variable, which will have value label attributes.

Usage

box_it(
  x,
  which = "top",
  number = 2,
  replace_na = NULL,
  lab_str = c("Selected", "Not selected", "Missing value"),
  var_label = paste0(which, number),
  na_val = 99
)

Arguments

x

Variable to be passed through

which

Character string to specify which end of spectrum to take values

  • valid inputs are "top" and "bottom".

number

integer value indicating the n to take values from, e.g. 'top n box' or 'bottom n box'

replace_na

numeric vector. These values from the variable are replaced with NA_real_.

lab_str

string vector of length 3 containing the labels for the output binary variable. The first value maps to 1 (box selected), the second value maps to 0 (box not selected), and the third values maps to missing values.

var_label

string to be used as the variable label, passed through to set_varl().

na_val

numeric value or NULL, defaults to 99. When set to a number, this is the number that is used to represent missing values. When set to NULL, missing values would be stored as NA_real_ type.

Value

a binary variable of labelled double type.

Examples

box_it(sample(1:10,100,replace = TRUE)) # Converted to binary variable where 9, 10 are selected
box_it(sample(c(1:10, NA),100,replace = TRUE))

# Example where specified numeric values are replaced with NAs
summary(
  box_it(
    sample(c(1:10, 99), 100, replace = TRUE),
    replace_na = 99)
  )


martinctc/surveytools documentation built on Nov. 21, 2024, 10:48 p.m.