auto.class: Automatically Classify A Vector As Numeric/Factor/Character

View source: R/auto.class.R

auto.classR Documentation

Automatically Classify A Vector As Numeric/Factor/Character

Description

Takes in a vector as input, coerces the vector into a specific class using as.numeric(), factor() or as.character(). Coerced vector is returned as output. The classification of the class is based on the following chain of logic:

  • If the vector contains elements with non-numeric characters and have less than X unique values, classify as factor.

  • If the vector contains elements with non-numeric characters and have more than X unique values, classify as character.

  • If the vector contains elements with only numeric characters and have more than X unique values, classify as numeric.

  • If the vector contains elements with only numeric characters and have less than X unique values, classify as factor.

Usage

auto.class(vector, unique.thres.fac = 20)

Arguments

vector

A vector to be classified.

unique.thres.fac

The maximum number of unique categories for a vector to be classified as a factor, X in the description takes this value. Defaults to 20.

Examples

# return numeric
auto.class(1:100)

# return factor
auto.class(rep(1:7,10))

# return character
auto.class(c(1:100,"hello"))

# return factor
auto.class(c("Strongly Agree","Agree","Disagree","Strongly Disagree"))

# use with lapply to apply across entire dataframe
mydata <- data.frame(A = 1:100, B = c(1:99,"hello"))
mydata <- data.frame(lapply(mydata, FUN = auto.class))
str(mydata)

Aaron0696/aaRon documentation built on July 27, 2023, 2:05 p.m.