Skip to content
Related Articles

Related Articles

Convert a Vector into Factor in R Programming – as.factor() Function
  • Last Updated : 04 Jun, 2020
GeeksforGeeks - Summer Carnival Banner

as.factor() function in R Language is used to convert the passed object(usually Vector) into a Factor.

Syntax: as.factor(object)

Parameters:
Object: Vector to be converted

Example 1:




# Creating a vector 
x<-c("female", "male", "male", "female"
  
# Using as.factor() Function
# to convert vector into factor
as.factor(x)

Output:



[1] female male   male   female
Levels: female male

Example 2:




# Creating a vector 
x<-c("North", "North", "East", "West"
  
# Using as.factor() Function
# to convert vector into factor
as.factor(x)

Output:

[1] North North East  West 
Levels: East North West

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :