Search code examples
rdataframetype-conversionnumericcategorical-data

Change Numeric Variables to be Categorical in R


Short and sweet here...

I have ZIP codes in the dataframe I am analyzing in R.

R is reading them as numeric variables. How can I have R read these ZIP codes (e.g., 20001) as a categorical variable.


Solution

  • (Just making my comment an answer)

    You can use

    data$ZIP <- as.character(data$ZIP) 
    

    or

    data$ZIP <- as.factor(data$ZIP)
    

    depending on the variable type you prefer to work with.