In R, a factor variable is defined as a categorical variable that can take on a limited set of values, known as levels. It is created using the `factor()` function, which takes a vector of categorical values and converts it into a factor variable. For example, the following code creates a factor variable called "gender" with two levels, "male" and "female":
```
gender <- factor(c("male", "female", "male", "male", "female"))
```
Once created, factor variables can be used in statistical analyses and visualizations to explore relationships between categorical variables.