R Factors

[This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A Factor is a data structure that is used to work with categorizable datas.

Suppose a data field such as marital status may contain only values from single, married, separated, divorced, or widowed.

In such a case, we know the possible values beforehand and these predefined, distinct values are called levels of a factor.


Create a Factor in R

In R, we use the factor() function to create a factor. Once a factor is created, it can only contain predefined set values called levels.

The syntax for creating a factor is

factor(vector)

Here, factor() takes a vector as an argument.

Let's see an example,

# create a factor
students_gender <- factor(c("male", "female", "male", "transgender", "female"))

# print the marital_status factor
print(students_gender)

Output

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

In the above example, we have used the factor() function to create the factor named students_gender.

Notice while printing students_gender, we get two outputs

  • All the vectors items
  • predefined possible values we know beforehand i.e. levels of students_gender

Access Factors Elements

Accessing vector elements is similar to that of vectors. We use the index number. For example,

# create a factor
students_gender <- factor(c("male", "female", "male", "transgender", "female"))

# access 1st element of students_gender
print(students_gender[1])

# access 4th element of students_gender
print(students_gender[4])

Output

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

In the above example, we have used the index number to access elements of the students_gender

  • students_gender[1] - returns the 1st element of students_gender i.e "male"
  • students_gender[4] - returns the 4th element of students_gender i.e. "transgender"

Note that each time we access and print factor elements we get a level of factor too.


Modify Factor Element

To change a vector element, we can simply reassign a new value to the specific index. For example,

# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

# print the marital_status factor
marital_status[1] <- "divorced"

print(marital_status[1])

Output

[1] divorced
Levels: divorced married single

Here, we have reassigned a new value to index 1 of the marital_status factor to change the element from "married" to "divorced".


Frequently Asked Questions

How to find the length of a factor in R?

In R, we use the length() function to find the number of items present in a factor. For example,

# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

cat("Total Elements:", length(marital_status))

Output

Total Elements: 5
How to loop over a factor in R?

In R, we can also loop through each element of the factor using the for loop. For example,

# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

# iterate through each elements of marital_status
for (status in marital_status) {
    print(status)
}

Output

[1] "married"
[1] "single"
[1] "single"
[1] "divorced"
[1] "married"
To leave a comment for the author, please follow the link and comment on their blog: R feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)