In R, factors are used to represent categorical variables. Categorical variables are those that take a limited number of possible values, called levels. Factors are useful when working with data frames and conducting data analysis tasks such as modeling.
To create a factor in R, you can use the factor()
function. Here's an example:
main.r229 chars9 lines
Output:
main.r104 chars3 lines
In the example above, we created a vector of vegetable names, and then used the factor()
function to create a factor from the vector. R automatically assigns levels to each unique value in the vector, and the resulting factor is displayed.
You can also specify the levels of a factor explicitly by passing them as arguments to the factor()
function. For example:
main.r241 chars9 lines
Output:
main.r63 chars3 lines
In the example above, we created a vector of color names and used factor()
to create a factor. We also specified the levels of the factor as "red", "green", and "blue" in that order. When the factor is displayed, the levels are shown in that order as well.
Factors are important to understand when working with data frames in R, since categorical variables are often represented as factors in data frames.
gistlibby LogSnag