Learn R Programming

SciencesPo (version 1.02.12)

factorize: Conditionally convert vectors to factors

Description

A generic function and several instances for creating factors from other sorts of data. The primary use case is for vectors that contain few unique values and might be better considered as factors. When applied to a data frame, this is applied to each column in the data.frame.

Usage

factorize(x, ...)

## S3 method for class 'default': factorize(x, ...)

## S3 method for class 'numeric': factorize(x, max.levels = 10L, ...)

## S3 method for class 'character': factorize(x, max.levels = 10L, ...)

## S3 method for class 'data.frame': factorize(x, max.levels = 10L, ...)

Arguments

x
an object.
...
additional arguments (currently ignored)
max.levels
an integer that determines the number of unique values to be coverted. Default is max.levels = 10.

encoding

UTF-8

Examples

Run this code
#Some data
ID = 1:10
Age = round(rnorm(10,50,1))
diag = c("Depression","Bipolar");
Diagnosis = sample(diag, 10, replace=TRUE)
data = data.frame(ID, Age, Diagnosis)
factorize(data$Diagnosis)
str(factorize(data))

Run the code above in your browser using DataLab