Learn R Programming

widyr (version 0.1.5)

widely_kmeans: Cluster items based on k-means across features

Description

Given a tidy table of features describing each item, perform k-means clustering using kmeans() and retidy the data into one-row-per-cluster.

Usage

widely_kmeans(tbl, item, feature, value, k, fill = 0, ...)

Arguments

tbl

Table

item

Item to cluster (as a bare column name)

feature

Feature column (dimension in clustering)

value

Value column

k

Number of clusters

fill

What to fill in for missing values

...

Other arguments passed on to kmeans()

See Also

widely_hclust()

Examples

Run this code

library(gapminder)
library(dplyr)

clusters <- gapminder %>%
  widely_kmeans(country, year, lifeExp, k = 5)

clusters

clusters %>%
  count(cluster)

# Examine a few clusters
clusters %>% filter(cluster == 1)
clusters %>% filter(cluster == 2)

Run the code above in your browser using DataLab