Does long format automatically recognize a factor variable?


When I reshape a wide format dataframe into a long format one, does it automatically recognize "ind" variable as a factor (if I use stack() function) or does it automatically recognize "variable" (the variable I set by "measured = " if I use melt() function) as a factor?

The textbook I use asks me to use factor() to make variable a factor, but I think it is recognized as a factor variable already. Could someone please explain to me?

Thank you.


You can check to see if a variable is recognized as a factor using is.factor().

For example:


is.factor(starwars$hair_color) # returns TRUE or FALSE

An alternative is to check the data types using the RStudio Environment tab, click on the small blue circle with white arrow to the left of the object and it will unfold the details of the data frame. In the case of this reprex data, the starwars$hair_color variable is not a factor, it is a character (chr). You can change it to a factor using starwars$hair_color <- as.factor(starwars$hair_color).

I hope this is helpful.