Search code examples
rdata-analysis

R: Factorize one variable in data frame


I have an R data frame with some variables that are stored as numeric but are actually categorical. How do I convert one specific column's data type of an existing data frame from numeric to factor?


Solution

  • You can use:

    dat$col <- as.factor(dat$col)
    

    where dat is the name of your data frame and col the name of the column.