df.make.factors <- function(df)
{
## Purpose: Convert all character variables in your data.frame to factors
## Arguments:
## df: a data frame
## Return: a data frame with converted factors on those character variables.
## Author: Feiming Chen, Date: 17 Oct 2017, 14:26
## ________________________________________________
character_vars <- sapply(df, class) == "character"
df[, character_vars] <- lapply(df[, character_vars], factor)
df
}
if (F) { # Unit Test
df <- data.frame(x=c("x", "x", "y"), y=1:3, z=c("1", "3", "3"))
df.make.factors(df)
}
Wednesday, October 18, 2017
Convert all character variables in your data.frame to factors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment