R Programming

How to create factor variables in R programming

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

Categorical variables, including nominal and ordinal variables in R programming language are called factor variables. For example, gender(male/female) is nominal, and survey results (excellent, good, normal, bad) have ordinal values. Categorical variables are useful because many data analysis operations are related to values in different categories, such as contingency tables between two categorical variables for independence analysis, hypothesis testing of homogeneity of variances, just name a few. So usually, the values of those categories must have to be set as factor type in R before the analysis can go further.

In R language, function factor() is used to set or create variable/vector as factor type. The basic form of the function is

factor(var, order)

Where var is a vector, and order is set to TRUE if the values in the vector are ordered.

The following example shows creation of a data frame, in which variable ‘blood’ is an unordered factor variable, and variable ‘status’ is an ordered variable.

#create variable vectors
ID <- c(3, 4, 5, 6) 
age <- c(15, 14, 18, 12)
blood <- c("Type5", "Type6", "Type5", "Type6")
status <- c("Poor", "Improved", "Excellent", "Poor")

# to set vector ‘blood’ as an factor without order
blood <- factor(blood)

# to set vector ‘status’ as ordered factor
status <- factor(status, order = TRUE)

# to create a data frame
pdata <- data.frame(ID, age, blood, status)   

#to show structure of data frame 
str(pdata)  
'data.frame': 4 obs. of  4 variables:
 $ ID    : num  3 4 5 6
 $ age   : num  15 14 18 12
 $ blood : Factor w/ 2 levels "Type5","Type6": 1 2 1 2
 $ status: Ord.factor w/ 3 levels "Excellent"<"Improved"<..: 3 2 1 3 

#get summary information of data frame
summary(pdata)

> summary(pdata)
       ID            age          blood         status 
 Min.   :3.00   Min.   :12.00   Type5:2   Excellent:1  
 1st Qu.:3.75   1st Qu.:13.50   Type6:2   Improved :1  
 Median :4.50   Median :14.50             Poor     :2  
 Mean   :4.50   Mean   :14.75                          
 3rd Qu.:5.25   3rd Qu.:15.75                          
 Max.   :6.00   Max.   :18.00                          
> 

You can also watch full video for R fundamental course from our YouTube channel.

wilsonzhang746

Recent Posts

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

1 day ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

2 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

11 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago

Python Machine Learning Source Files

Click here to download Python Machine Learning Source Files !

1 year ago

Install PyTorch on Windows

PyTorch is a deep learning package for machine learning, or deep learning in particular for…

1 year ago