Learn R Programming

REDCapCAST (version 24.12.1)

var2fct: Convert vector to factor based on threshold of number of unique levels

Description

This is a wrapper of forcats::as_factor, which sorts numeric vectors before factoring, but levels character vectors in order of appearance.

Usage

var2fct(data, unique.n)

Value

vector

Arguments

data

vector or data.frame column

unique.n

threshold to convert class to factor

Examples

Run this code
sample(seq_len(4), 20, TRUE) |>
  var2fct(6) |>
  summary()
sample(letters, 20) |>
  var2fct(6) |>
  summary()
sample(letters[1:4], 20, TRUE) |> var2fct(6)

Run the code above in your browser using DataLab