What are factors in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What are factors in R?

What are factors in R?

This recipe explains what are factors in R

0

Recipe Objective

What are factors in R ? Factors () are data objects that categorize the data or represent the categorical data, in order to store that data on multiple levels. The distinct values in factors are called levels. Factors can store integer as well as string values. This recipe demonstrates an example of factors with vectors as input.

Step 1 - Create a input vector

Enter all the categorical values in a input vector x

x <- c("good","better","good","best","bad","bad")

Step 2 - Apply factor()

Apply factor() function to x and storein output vector y

y <- factor(x)

Step 3 - Print output

Print y . The 'factors' are all values of input range and 'Levels' are the distinct values of the factors.

print(y)
 
"Output of the code is":

 good   better good   best   bad    bad   
Levels: bad best better good

To check if the value is factor or not , is.factor() can be used , if it returns TRUE, the value is a factor, if it returns FALSE, the value is not a factor.

is.factor(y)
"Output: TRUE" 

Relevant Projects

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.