Factors in R

Factors in R:

Factors in R is a data type used to store categorical variable, which can be either numeric or string. Most important advantage to convert integer or character to factor is, they can be used in statistical modeling where they will be implemented correctly. factor() is the function used to convert numerical or character variable to factor

We can also change the order of the factor according to our desire. Add an optional parameter called Levels within that create a vector of your desire order and add an another optional parameter ordered=TRUE.

Now lets look at the example

# Factors in R: Convert to Factor without order.
data = c(4,5,5,4,4,3,5,4,5,4,5,4,3,3)
fdata = factor(data) # converting numeric to factor

# Factors in R: Convert to Factor with desired order.


When we execute the above code, it produces the following result −

[1] 4 5 5 4 4 3 5 4 5 4 5 4 3 3

Levels: 3 4 5


friday    monday  saturday    sunday  thursday   tuesday wednesday

1               1               1               1               2                2              3


sunday    monday   tuesday wednesday  thursday    friday  saturday

1                1                2                3                2                1            1

In the above output you can see the difference between week (which has no order) and week_ordered (which has specific order)


Generating Factor Levels:

We can generate factor levels by using the gl() function. It takes two integers as input one indicates number of levels and other indicates the number of repetition.


gl(n, k, labels)

Following is the description of the parameters used:

  • n is a integer giving the number of levels.
  • k is a integer giving the number of replications.
  • labels is a vector of labels for the resulting factor levels


v <- gl(3, 4, labels = c("India", "USA", "Russia"))

When we execute the above code, it produces the following result


[1] India  India  India  India  USA    USA    USA    USA    Russia Russia

Russia Russia

Levels: India USA Russia

previous-small factor in Rnext_small factor in R


  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    https://www.linkedin.com/in/sridhar-venkatachalam-74414252/ karaimadai@sg2plcpnl0204.prod.sin2.secureserver.net Venkatachalam Sridhar