Este dilema se puede solucionar de diferentes formas, sin embargo te compartimos la solución más completa para nosotros.
Solución:
Hola bienvenido al mundo de R.
mtcars #look at this built in data set
str(mtcars) #allows you to see the classes of the variables (all numeric)
#one approach it to index with the $ sign and the as.factor function
mtcars$am <- as.factor(mtcars$am)
#another approach
mtcars[, 'cyl'] <- as.factor(mtcars[, 'cyl'])
str(mtcars) # now look at the classes
Esto también funciona para caracteres, fechas, números enteros y otras clases.
Como eres nuevo en R, te sugiero que eches un vistazo a estos dos sitios web:
R manuales de referencia:
http://cran.r-project.org/manuals.html
R Tarjeta de referencia: http://cran.r-project.org/doc/contrib/Short-refcard.pdf
# To do it for all names
df[] <- lapply( df, factor) # the "[]" keeps the dataframe structure
col_names <- names(df)
# to do it for some names in a vector named 'col_names'
df[col_names] <- lapply(df[col_names] , factor)
Explicación. Todos los marcos de datos son listas y los resultados de [
used with multiple valued arguments are likewise lists, so looping over lists is the task of lapply
. The above assignment will create a set of lists that the function data.frame.[<-
should successfully stick back into into the dataframe, df
Another strategy would be to convert only those columns where the number of unique items is less than some criterion, let's say fewer than the log of the number of rows as an example:
cols.to.factor <- sapply( df, function(col) length(unique(col)) < log10(length(col)) )
df[ cols.to.factor] <- aplicación(df[ cols.to.factor] factor)
podrías usar dplyr::mutate_if()
para convertir todas las columnas de caracteres o dplyr::mutate_at()
para seleccionar columnas de caracteres con nombre a factores:
library(dplyr)
# all character columns to factor:
df <- mutate_if(df, is.character, as.factor)
# select character columns 'char1', 'char2', etc. to factor:
df <- mutate_at(df, vars(char1, char2), as.factor)