The factors are the variable in R, which takes the categorical variable and stores data in levels. Factors can be ordered or unordered and are an important class for statistical analysis and for plotting.
factor is used to encode a vector as a factor. Therefore, If the argument
TRUE, the factor levels are assumed to be ordered.
as.ordered are the membership and coercion functions for these classes.
Factors are created using the
factor () function by taking a vector as input.
# let's create a vector > logical <- c("TRUE", "FALSE", "TRUE", "TRUE", "FALSE") > logical.factor <- factor(logical) >logical.factor  TRUE FALSE TRUE TRUE FALSE Levels: FALSE TRUE > levels(factor(logical))  "FALSE" "TRUE"
Therefore, ‘Levels’ help us to sort alphabetically.
Structure of factor function
So, we can see the structure of factor with
> str(logical.factor) Factor w/ 2 levels "FALSE","TRUE": 2 1 2 2 1
Changing the Order of Levels
Moreover, we can change the order of levels with applying factor function with the new order of levels.
# Create a vector > data <- c("East","South","East","North","North","West","West","East","North") # Create the factors > data.factor <- factor(data) > print(factor_data)  East South East North North West West East North Levels: East North South West # Apply the factor function with required order of the level. > data.order <- factor(data.factor, levels = c("North", "South", "East", "West")) > print(new_order_data)  East South East North North West West East North Levels: North South East West
Generating Factor Levels
Furthermore, we can generate factor levels by using the
gl() function. In additon it takes two integers as input which indicates how many levels and how many times each level.
gl(n, k, labels)
- n is an integer giving the number of levels.
- k is an integer giving the number of replications.
- labels is a vector of labels for the resulting factor levels.
> factor_lvl <- gl(2, 3, labels = c("TRUE", "FALSE")) > factor_lvl  TRUE TRUE TRUE FALSE FALSE FALSE Levels: TRUE FALSE
Renaming a Factor levels
Similarly, we can change the name of the vector values in the input by specifying the regular use of ‘levels’ as the first argument with values “TRUE” and “FALSE” and can change the vector values using ‘labels’ as the second argument with “T” and “F” respectively.
> logical <- c("TRUE", "FALSE", "TRUE", "TRUE", "FALSE") > logical.factor <- factor(logical) > new_label <- factor(logical.factor, levels=c("TRUE","FALSE"),labels=c("M","F"))
In conclusion we studeid what is factor how to use factor, what is the structure of factor, changing the oder of levels genreating factor levels along with the renaming of factor levels, as shown above.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!