Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

numeric_to_factor

Convert numeric vectors into factors associated value labels


Description

This function converts numeric variables into factors, and uses associated value labels as factor levels.

Usage

numeric_to_factor(x, n = 4)

Arguments

x

A data frame.

n

Numeric, indicating the maximum amount of unique values in x to be considered as "factor". Variables with more unique values than n are not converted to factor.

Details

If x is a labelled vector, associated value labels will be used as level. Else, the numeric vector is simply coerced using as.factor().

Value

x, with numeric values with a maximum of n unique values being converted to factors.

Examples

library(dplyr)
data(efc)
efc %>%
  select(e42dep, e16sex, c12hour, c160age, c172code) %>%
  numeric_to_factor()

sjmisc

Data and Variable Transformation Functions

v2.8.6
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>), Iago Giné-Vázquez [ctb], Alexander Bartel [ctb] (<https://orcid.org/0000-0002-1280-6138>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.