[Skip to content](/content/2017/03/23/clustering-in-r/#content/index.html)

help(kmeans)

```
set.seed(1)
x=matrix(rnorm(50*2), ncol=2)
plot(x)
```

```
x[1:25,1]=x[1:25,1]+3
x[1:25,2]=x[1:25,2]-4
plot(x)
```

```
km.out=kmeans(x,2,nstart=20)
km.out
```

```
K-means clustering with 2 clusters of sizes 25, 25

Cluster means:
        [,1]        [,2]
1 0.03223135  0.06924384
2 3.16866521 -3.83459093

Clustering vector:
 [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Within cluster sum of squares by cluster:
[1] 28.53417 50.97988
 (between_SS / total_SS =  79.8 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"
[7] "size"         "iter"         "ifault"
```

```
km.out$cluster
```

```
 [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
```

```
plot(x, col=(km.out$cluster +1), main="K-Means Clustering Results with K=2", xlab="", ylab="", pch=20, cex=2)
points(km.out$centers,pch=8,cex=2)
```

```
set.seed (2)
km.out=kmeans(x,3,nstart=20)
km.out
```

```
K-means clustering with 3 clusters of sizes 25, 20, 5

Cluster means:
        [,1]        [,2]
1 0.03223135  0.06924384
2 3.08290361 -4.26589906
3 3.51171162 -2.10935842

Clustering vector:
 [1] 2 2 2 2 3 3 2 2 2 2 3 2 2 2 2 2 2 3 2 3 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Within cluster sum of squares by cluster:
[1] 28.534174 27.901401  3.740301
 (between_SS / total_SS =  84.7 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"
[7] "size"         "iter"         "ifault"
```

```
set.seed (3)
km.out=kmeans(x,3,nstart=1)
km.out$tot.withinss
```

```
[1] 60.56297
```

```
km.out=kmeans(x,3,nstart=20)
km.out$tot.withinss
```

```
[1] 60.37249
```

```
iris2 = iris
iris2$Species = NULL
kmeans.result = kmeans(iris2,3,nstart=20)
kmeans.result
```

```
K-means clustering with 3 clusters of sizes 38, 50, 62

Cluster means:
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1     6.850000    3.073684     5.742105    2.071053
2     5.006000    3.428000     1.462000    0.246000
3     5.901613    2.748387     4.393548    1.433871

Clustering vector:
  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [51] 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[101] 1 3 1 1 1 1 3 1 1 1 1 1 1 3 3 1 1 1 1 3 1 3 1 3 1 1 3 3 1 1 1 1 1 3 1 1 1 1 3 1 1 1 3 1 1 1 3 1 1 3

Within cluster sum of squares by cluster:
[1] 23.87947 15.15100 39.82097
 (between_SS / total_SS =  88.4 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"
[7] "size"         "iter"         "ifault"
```

```
table(iris$Species, kmeans.result$cluster)
```

```

1  2  3
  setosa      0 50  0
  versicolor  2  0 48
  virginica  36  0 14
```

```
plot(iris2[c("Sepal.Length", "Sepal.Width")], col = kmeans.result$cluster)
points(kmeans.result$centers[,c("Sepal.Length", "Sepal.Width")], col = 1:3,pch = 8, cex=2)
```

```
library(cluster)
help(pam)
 pam.result = pam(iris2,3)
 pam.result
```

```
Medoids:
      ID Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,]   8          5.0         3.4          1.5         0.2
[2,]  79          6.0         2.9          4.5         1.5
[3,] 113          6.8         3.0          5.5         2.1
Clustering vector:
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [51] 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[101] 3 2 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3 3 2
Objective function:
    build      swap
0.6709391 0.6542077

Available components:
 [1] "medoids"    "id.med"     "clustering" "objective"  "isolation"  "clusinfo"   "silinfo"
 [8] "diss"       "call"       "data"
```

```
 table(pam.result$clustering,iris$Species)
```

```

setosa versicolor virginica
  1     50          0         0
  2      0         48        14
  3      0          2        36
```

```
 plot(pam.result)
```

```
 library(RWeka)
 help(SimpleKMeans)
```

### Rate this:

i

Rate This

### Share this:

- [Share on WhatsApp (Opens in new window)WhatsApp](/content/2017/03/23/clustering-in-r/?share=jetpack-whatsapp&nb=1/index.html)
- Post Button

- [Email a link to a friend (Opens in new window)Email](mailto:?subject=%5BShared%20Post%5D%20Clustering%20in%20R&body=https%3A%2F%2Freckonanalytics.com%2F2017%2F03%2F23%2Fclustering-in-r%2F&share=email&nb=1)

LikeLoading...

[Reblog](https://widgets.wp.com/likes/index.html?ver=20260614# "Reblog this post on your main site.")

[Like](https://widgets.wp.com/likes/index.html?ver=20260614# "Be the first to like this.")

Be the first to like this.

### _Related_

#### [Using R in Tableau](/content/2017/05/25/using-r-in-tableau/ "Using R in&nbsp;Tableau"/index.html)

Requirements: Tableau Professional Edition R Studio or R Console Setting Up Tableau Desktop with R: Install following packages in R: install.packages(RServe) Once it is installed then start R Server by entering following command in the console: > library(RServe) > RServe() Configuring Rserve Connection: In Tableau go to following menu setting:…

May 25, 2017

In "R"

#### [Six Simple Steps to Improve Stored Procedures in SQL Server](/content/2019/09/05/six-simple-steps-to-improve-stored-procedures-in-sql-server/ "Six Simple Steps to Improve Stored Procedures in SQL&nbsp;Server"/index.html)

1\. SET NOCOUNT ON: Stop the message that shows the count of rows affected by a T-SQL statments. These messages can be suppressed with SET NOCOUNT ON and can increase performance by decreasing network traffic 2. IF EXISTS(): Use IF EXISTS() to check if data exist in a table or…

September 5, 2019

In "SQL"

#### [QVC](/content/2017/05/01/qvc/ "QVC"/index.html)

var divElement = document.getElementById('viz1493909286527'); var vizElement = divElement.getElementsByTagName('object')\[0\]; vizElement.style.width='100%';vizElement.style.height=(divElement.offsetWidth\*0.75)+'px'; var scriptElement = document.createElement('script'); scriptElement.src = 'https://public.tableau.com/javascripts/api/viz\_v1.js'; vizElement.parentNode.insertBefore(scriptElement, vizElement); The objective of this challenge was to create a Decision Support System so that QVC can make strategic decisions to answer some of the business questions such as customer next buy (product…

May 1, 2017

In "Tableau"

### Leave a comment [Cancel reply](/content/2017/03/23/clustering-in-r/\#respond/index.html)

Write a comment...

Log in or provide your name and email to leave a comment.

Email me new posts

InstantlyDailyWeekly

Email me new comments

Save my name, email, and website in this browser for the next time I comment.

Comment

Δ

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)

- [Comment](/content/2017/03/23/clustering-in-r/#respond/index.html)
- [Reblog](/content/2017/03/23/clustering-in-r/index.html)
- [Subscribe](/content/2017/03/23/clustering-in-r/index.html) [Subscribed](/content/2017/03/23/clustering-in-r/index.html)

-  Reckon Analytics](/content/site-root.html)

Sign me up

- Already have a WordPress.com account? [Log in now.](https://wordpress.com/log-in?redirect_to=https%3A%2F%2Fr-login.wordpress.com%2Fremote-login.php%3Faction%3Dlink%26back%3Dhttps%253A%252F%252Freckonanalytics.com%252F2017%252F03%252F23%252Fclustering-in-r%252F)

- -  Reckon Analytics](/content/site-root.html)
  - [Subscribe](/content/2017/03/23/clustering-in-r/index.html) [Subscribed](/content/2017/03/23/clustering-in-r/index.html)
  - [Sign up](https://wordpress.com/start/)
  - [Log in](https://wordpress.com/log-in?redirect_to=https%3A%2F%2Fr-login.wordpress.com%2Fremote-login.php%3Faction%3Dlink%26back%3Dhttps%253A%252F%252Freckonanalytics.com%252F2017%252F03%252F23%252Fclustering-in-r%252F)
  - [Copy shortlink](https://wp.me/p70PoO-6o)
  - [Report this content](https://wordpress.com/abuse/?report_url=https://reckonanalytics.com/2017/03/23/clustering-in-r/)
  - [View post in Reader](https://wordpress.com/reader/blogs/103631934/posts/396)
  - [Manage subscriptions](https://subscribe.wordpress.com/)
  - [Collapse this bar](/content/2017/03/23/clustering-in-r/index.html)

%d

Twitter Widget Iframe
