Creating a Factor in R Programming Language
The command used to create or modify a factor in R language is – factor() with a vector as input.
The two steps to creating an R factor :
- Creating a vector
- Converting the vector created into a factor using function factor()
Examples: Let us create a factor gender with levels female, male and transgender.
R
# Creating a vector x <- c ( "female" , "male" , "male" , "female" ) print (x) # Converting the vector x into a factor # named gender gender <- factor (x) print (gender) |
Output
[1] "female" "male" "male" "female" [1] female male male female Levels: female male
Levels can also be predefined by the programmer.
R
# Creating a factor with levels defined by programmer gender <- factor ( c ( "female" , "male" , "male" , "female" ), levels = c ( "female" , "transgender" , "male" )); gender |
Output
[1] female male male female Levels: female transgender male
Further one can check the levels of a factor by using function levels().
R Factors
Factors in R Programming Language are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels.
They can be stored as integers with a corresponding label to every unique integer. The R factors may look similar to character vectors, they are integers and care must be taken while using them as strings. The R factor accepts only a restricted number of distinct values. For example, a data field such as gender may contain values only from female, male, or transgender.
In the above example, all the possible cases are known beforehand and are predefined. These distinct values are known as levels. After a factor is created it only consists of levels that are by default sorted alphabetically.
Contact Us