## R Factor

Factors are used to describe items that can have a finite number of values. A factor can store both strings and integers. Factors are useful when limited unique values in the columns. Like “Male”, “Female” and True,False etc. Factors useful in data analysis for statistical modeling.

Factors are defined using factor() function. Which takes vector as input.

## Create Factors in R:

> data1<-c("married","single","widow","divorced","widow","divorced","married","single") #Create Vector > data1 [1] "married" "single" "widow" "divorced" "widow" "divorced" "married" "single" > factor_data1<-factor(data1) #create factor using vector data1 > is.factor(factor_data1) #Check factor or not [1] TRUE > factor_data1 # To checks levels in factor. [1] married single widow divorced widow divorced married single Levels: divorced married single widow > class(factor_data1) #Check factor or not [1] "factor"