Factors

Factors are used to represent categorical data and can be unordered or ordered. An example might be “Male” and “Female” if we consider gender. Factor objects can be created with the factor() function.

 

x <- factor(c("male", "female", "male", "male", "female"))
x
table(x)

Output:

> x
[1] male female male male female
Levels: female male
> table(x)
x
female male
2 3

By default Levels are put in alphabetical order. If you print the above code you will get levels as female and male. But if you want to get your levels in particular order then set levels parameter like this.

 

x <- factor(c("male", "female", "male", "male", "female"), levels=c("male", "female"))
x
table(x)

Output:

> x
[1] male female male male female
Levels: male female
> table(x)
x
male female
3 2

Extract Elements from a Factor:

Accessing elements of a factor is similar to accessing from vectors.

 

x <- factor(c("male", "female", "male", "male", "female"))
x[1] #Access only the first element
x[c(2,3)] #Access 2nd amd 3rd elements.
x[3:5] #Access from 3rd to 5th elements
x[-1] #Access all elements except 1st one

Output:

x[1] #Access only the first element
[1] male
Levels: female male
> x[c(2,3)] #Access 2nd amd 3rd elements.
[1] female male
Levels: female male
> x[3:5] #Access from 3rd to 5th elements
[1] male male female
Levels: female male
> x[-1] #Access all elements except 1st one
[1] female male male female
Levels: female male

Modifying a factor in R:

Elements of a factor can be modified using assignment operator. Just assign some new value. But note that, you cannot choose values outside of its predefined levels.

x[1]<-"female"
x

Output:

[1] female female male male female
Levels: female male

You can add a new level with the existing one like below example.

levels(x) <- c(levels(x), "Third Gender")
levels(x)

Output:

[1] "female"       "male"         "Third Gender"

Lists

Matrices