Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 08 Nov, 2021

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

Syntax: as.factor(object)

Parameters: 

  • Object: Vector to be converted

as.factor() Function in R Example

Example 1: Convert a Factor in R

R




# 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: Using as.factor() function to the character object containing numbers

Here we are applying this function to the integer vector 

R




data <- c("11.1", "3.11", "32.2", "2.2")
 
as.factor(data)

Output: 

[1] 11.1 3.11 32.2 2.2 
Levels: 11.1 2.2 3.11 32.2
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!