auto.class | R Documentation |
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.
auto.class(vector, unique.thres.fac = 20)
vector |
A vector to be classified. |
unique.thres.fac |
The maximum number of unique categories for a vector to be classified as a factor, |
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.