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.

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
#> Converted to binary variable where 9, 10 are selected
#> <labelled<double>[100]>
#>   [1] 1 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
#>  [38] 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0
#>  [75] 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
#> 
#> Labels:
#>  value         label
#>      1      Selected
#>      0  Not selected
#>     99 Missing value
box_it(sample(c(1:10, NA),100,replace = TRUE))
#> Converted to binary variable where 9, 10 are selected
#> <labelled<double>[100]>
#>   [1]  1  0  1  1  1  0  0  0  0  0  0  0  1  0  0  0  1  0  0 NA  0  0  0  0  0
#>  [26]  1  0 NA  0  0  0  1  0  0  1  0  0  0  0  0 NA  0  0 NA  1 NA  0 NA NA  1
#>  [51]  0  0  0  0  1  0  0  0  0  0  0  0  1  0  0  0  1  0  0  0  1 NA  0  0  1
#>  [76]  0  0  0  1  0  0  0  0  0  0  0  0  0 NA  0  0  0  1  1  0  1 NA  0  0 NA
#> 
#> Labels:
#>  value         label
#>      1      Selected
#>      0  Not selected
#>     99 Missing value

# Example where specified numeric values are replaced with NAs
summary(
  box_it(
    sample(c(1:10, 99), 100, replace = TRUE),
    replace_na = 99)
  )
#> Converted to binary variable where 9, 10 are selected
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#>  0.0000  0.0000  0.0000  0.2111  0.0000  1.0000      10