augment: Augment data with predictions

augment.cluster_fitR Documentation

Augment data with predictions

Description

augment() will add column(s) for predictions to the given data.

Usage

## S3 method for class 'cluster_fit'
augment(x, new_data, ...)

Arguments

x

A cluster_fit object produced by fit.cluster_spec() or fit_xy.cluster_spec().

new_data

A data frame or matrix.

...

Not currently used.

Details

For partition models, a .pred_cluster column is added.

Preprocessing with workflows

When x is a fitted workflows::workflow() that includes a recipe, the recipe transformations are applied to new_data before predicting. The returned tibble contains the original (untransformed) new_data plus the .pred_cluster column, so the data is not altered by preprocessing.

Value

A tibble containing new_data with a .pred_cluster column appended giving the cluster assignment for each row.

Examples

kmeans_spec <- k_means(num_clusters = 5) |>
  set_engine("stats")

kmeans_fit <- fit(kmeans_spec, ~., mtcars)

kmeans_fit |>
  augment(new_data = mtcars)

# With a workflow that includes a recipe
library(recipes)
library(workflows)

rec <- recipe(~., data = mtcars) |>
  step_normalize(all_predictors())

wf_fit <- workflow() |>
  add_recipe(rec) |>
  add_model(kmeans_spec) |>
  fit(data = mtcars)

# Returns original (untransformed) data with .pred_cluster appended
augment(wf_fit, new_data = mtcars)

tidyclust documentation built on June 20, 2026, 9:08 a.m.