Open In App

Data Mining in R

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Data mining is the process of discovering patterns and relationships in large datasets. It involves using techniques from a range of fields, including machine learning, statistics and database systems, to extract valuable insights and information from data.

In this article, we will provide an overview of data mining in the R Programming Language, including some of the most commonly used techniques and tools. We will begin by introducing the basics of data mining and R and then move to more advanced topics such as machine learning and text mining.

Getting Started with Data Mining in R

To begin working with data mining in R:

1. Install R and an IDE

Install R and a development environment such as RStudio, which provides an intuitive interface for coding, visualization and package management.

2. Install Key Packages

Install commonly used packages using install.packages() and load them with library(). We will install caret ,dplyr and ggplot2 .

R
install.packages("caret")
install.packages("dplyr")
install.packages("ggplot2")
library(dplyr)
library(caret)
library(ggplot2)

Data Mining Techniques

We will explore some commonly used data mining techniques and there implementation.

1. Clustering

Clustering can be used for a variety of purposes, including discovering hidden patterns in the data, summarizing data and generating new features. This involves partitioning a dataset into groups (called clusters) such that the observations within each cluster are more similar to each other than they are to observations in other clusters.

R
install.packages("factoextra")
library(factoextra)

df <- mtcars
df <- na.omit(df)
df <- scale(df)

# With 4 Centers
km <- kmeans(df, centers = 4, nstart = 25)
fviz_cluster(km, data = df)

# With 5 Centers
km <- kmeans(df, centers = 5, nstart = 25)
fviz_cluster(km, data = df)

Output:

Clusters formed using 4 cluster centers
Clusters formed using 4 cluster centers
Clusters formed using 5 cluster centers
Clusters formed using 5 cluster centers

2. Association Rule

Association Rule is used to identify patterns or relationships within large datasets, such as products frequently bought together in retail. Rules are expressed as if-then statements, where the presence of one item suggests the presence of another. The strength of these associations is measured by support, confidence and lift, helping businesses optimise product placement, promotions and recommendations. It is widely applied in market basket analysis and recommendation systems.

R
install.packages("arules")
install.packages("arulesViz")
library(arules)
library(arulesViz)

data("Groceries")

rules <- apriori(Groceries, parameter = list(support = 0.01, confidence = 0.5))

inspect(head(rules, 5))

plot(rules, method = "grouped")

Output:

AssociationRuleTable
Table
PlotofAR
Plot

3. Regression

Linear Regression is one of the most widely used regression techniques to model the relationship between two variables. It uses a linear relationship to model the regression line. There are 2 variables used in the linear relationship equation i.e., the predictor variable and the response variable.

Linear Regression Equation:

y = a x + b

where,

  • x indicates predictor or independent variable
  • y indicates response or dependent variable
  • a and b are coefficients
R
x <- c(153, 169, 140, 186, 128, 136, 178, 163, 152, 133)
y <- c(64, 81, 58, 91, 47, 57, 75, 72, 62, 49)

model <- lm(y ~ x)
print(model)

df <- data.frame(x = 182)
res <- predict(model, df)
cat("\nPredicted value of a person with height = 182\n")
print(res)

plot(x, y, main = "Height vs Weight Regression Model")
abline(model)

Output:

Linear_reg
Model and Prediction
Regression line with the points on a 2D plot
Regression line with the points on a 2D plot

4. Classification

Classification is a supervised learning technique used to categorize data into predefined classes based on input features. It is widely used in applications such as spam detection, disease diagnosis and image recognition.

R
install.packages("randomForest")
library(randomForest)

data(iris)

set.seed(123)

model <- randomForest(Species ~ ., data = iris, ntree = 100)

pred <- predict(model, iris)

# Confusion matrix
table(Predicted = pred, Actual = iris$Species)

Output:

Rf_clf
RF CLassfication

Next Article

Similar Reads