Convert a Vector into Factor in R Programming – as.factor() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] female male   male   female
Levels: female male

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] North North East  West 
Levels: East North West
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.