Skip to content

Instantly share code, notes, and snippets.

@somerealnumbers
Created July 10, 2016 08:53
Show Gist options
  • Select an option

  • Save somerealnumbers/4f5433ae088f48e2a4861204f4a7cb5c to your computer and use it in GitHub Desktop.

Select an option

Save somerealnumbers/4f5433ae088f48e2a4861204f4a7cb5c to your computer and use it in GitHub Desktop.
#################################################
# un-parameterised
require(dplyr)
df = iris[,sapply(X = iris
, function(X) is.numeric(X))]
df.cluster = df %>% kmeans(x = .
, centers = 4)
df %>% plot(main = '4 Cluster Solution'
, col = df.cluster$cluster)
#################################################
# partially parameterised
require(dplyr)
nClust = 4
df = iris[,sapply(X = iris
, function(X) is.numeric(X))]
df.cluster = df %>% kmeans(x = .
, centers = nClust)
df %>% plot(main = paste(nClust, 'Cluster Solution')
, col = df.cluster$cluster)
#################################################
# parameterised
find_clusters = function(inputData, nClust){
df = inputData[,sapply(X = inputData
, function(X) is.numeric(X))]
df.cluster = df %>% kmeans(x = .
, centers = nClust)
df %>% plot(main = paste(nClust, 'Cluster Solution')
, col = df.cluster$cluster)
}
find_clusters(inputData = iris, nClust = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment