Factors

Introduction

In the previous post, we learnt about dataframes. In this post, we will learn about factors.

  • create factors
  • order levels
  • specify labels
  • check levels
  • number of levels

Categorical or qualitative data in R is treated as data type factor.

Create Factors

args(factor)
## function (x = character(), levels, labels = levels, exclude = NA, 
##     ordered = is.ordered(x), nmax = NA) 
## NULL
devices <- factor(c('Mobile', 'Tablet', 'Desktop'))
devices
## [1] Mobile  Tablet  Desktop
## Levels: Desktop Mobile Tablet
# number of levels
nlevels(devices)
## [1] 3
# levels
levels(devices)
## [1] "Desktop" "Mobile"  "Tablet"

We will learn more about factors in a later post.