Grouping in R | Beginner's Guide to Clustering in R

Contents

vista-view-of-the-newly-discovered-globular-cluster-vvv-cl001-an

R Are you ready? Let's learn to group in R.

http: // www.pags: //www.rstudio.com/products/rstudio/download/

Data visualization using R

In present times, pictures speak louder than numbers or word analysis. Yes, graphs and diagrams are more attractive and easy to identify for the human eye. This is where the importance of R data analysis comes in.. Clients better understand the graphical representation of their growth / evaluation / product distribution. Therefore, data science is booming nowadays and R is one of those languages ​​that provides flexibility in plotting and graphics, as it has specific functions and packages for such tasks. RStudio is software where data and visualization happen side by side, which makes it very favorable for a data analyst. Scatter diagrams, box plots, bar graphs, line charts, line charts, heat maps, etc. are possible in R with just a simple function, for instance: the histogram can be plotted using the hist function (data name) with parameters as xlab (x tag), color, should, etc.

Taking advantage of this convenience, let's move to a method of Unsupervised learning: clustering.

Supervised and unsupervised learning

There are two types of learning in data analysis: supervised learning and unsupervised.

Supervised learning – The tagged data is an input to the learning machine. Regression, the classification, decision trees, etc. are supervised learning methods.

Supervised learning example:

Linear regression is where there is only one variable dependent. Equation: y = mx + c, y depends on x.

For instance: the age and girth of a tree are the 2 labels as input dataset, the machine needs to predict the age of a tree with a circumference as input after knowing the data set that was fed. Age depends on circumference.

Therefore, learning is monitored on the basis of labels.

Unsupervised learning – The unlabeled data is sent to the machine to find a pattern on its own. Clustering is an unsupervised learning method that has models: KMeans, hierarchical grouping, DBSCAN, etc.

The visual representation of the clusters shows the data in an easily understandable format, as it groups elements of a large data set according to their similarities. This makes analysis easier. But nevertheless, unsupervised learning is not always accurate and is a complex process for the machine, since the data is not labeled.

Let's now continue with an example of grouping using the Iris flower dataset.

Grouping

Clusters they are a group of the same elements or elements such as a cluster of stars or a cluster of grapes or a cluster of nets and so on …

Using clustering in the real world:

It is used in e-commerce sites to form customer groups based on their profile such as age, sex, spending, regularity, etc. It is useful in marketing and sales, as it helps to group the target audience of the product. Spam filtering in emails and many more are real-world clustering applications.

Clustering in R refers to the assimilation of the same type of data into groups or clusters to distinguish one group from the others. (collection of the same type of data). This can be represented in graphical format through R. We use the KMeans model in this process.

What is the K Means algorithm?

K Means is a clustering algorithm that repeatedly assigns a group among the k groups present to a data point according to the characteristics of the point. It is a grouping method based on centroids.

The number of clusters is decided, cluster centers are randomly selected farthest from each other, the distance between each data point and the center is calculated using the Euclidean distance, the data point is assigned to the cluster whose center is closest to that point. This process is repeated until the center of the groups does not change and the data points remain in the same group..

This is all theory, but in practice, R has a bundling package that calculates the steps above.

Paso 1

I will work on the Iris dataset, which is a built-in dataset in R using the Cluster package. Has 5 columns, namely: sepal length, sepal width, petal length, petal width and species. Iris is a flower and here in this dataset they are mentioned 3 of its species Setosa, Versicolor, Verginica. We will group the flowers according to their species. The code to load the dataset:

data("iris")
head(iris) #will show top 6 rows only
63849rstudio2026-04-20212022_31_04-4835929

Paso 2

The next step is to separate the columns 3 Y 4 in a separate x object, since we are using the unsupervised learning method. We are removing labels for the machine to use the huge input of petal length and width columns for unattended grouping.

x = iris[,3:4] #using only petal length and width columns
head(x)
39142rstudio2026-04-20212022_45_08-2883809

Paso 3

The next step is to use the K Means algorithm. K Means is the method we use that has parameters (data, no. From clusters to groups). Here our data is the object x and we will have k = 3 groups, since there are 3 species in the dataset.

So he ‘cluster package is named. Clustering in R is done using this built-in package which will do all the math. The Clusplot function creates a 2D plot of the clusters.

model=kmeans(x,3)
 library(cluster)
clusplot(x,model$cluster)
67391rstudio2026-04-20212022_58_36-4910374

The component 1 and the component 2 seen in the graph are the two components of PCA (principal component analysis), which is basically a feature extraction method that uses the important components and removes the rest. Reduces the dimensionality of the data to facilitate the application of KMeans. All of this is done by the package of cluster an R.

These two components explain the variability of the 100% at the exit, which means data object x fed to PCA was accurate enough to form clear groups using KMeans and there is minimal overlap (insignificant) among them.

Paso 4

The next step is to assign different colors to the groups and shade them, Thus, we use the color and shadow parameters by setting them to T, what true means.

clusplot(x,model$cluster,color=T,shade=T)
42154rstudio2026-04-20212023_04_33-9451716

Conclution

This all sums up the basics of clustering in R. Here I use a built-in dataset, but imported datasets can also be used for clustering. For instance: group site users based on favored items, etc. It is very useful for making business comparisons.

Import datasets into R:

dataset <- read.csv("path.csv") 
View(dataset)
attach(dataset)

Thanks for taking the time and reading this article.,Feel free to comment what can be improved, since learning is a daily process.aftereverybody..

ConnectwithmeaboutLinkedIn:https://www.linkedin.com/in/akansha-bose-149b14164/

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.

Datapeaker