Learn R Programming

twidlr (version 0.0.0.9000)

kmeans: data.frame-first formula-second method for

Description

This function passes a data.frame, formula, and additional arguments to kmeans.

Usage

kmeans(data, formula = ~., ...)

# S3 method for kmeans predict(object, data, ...)

Arguments

data
Data frame (or object that can be coerced to one) containing the variables in the model
formula
A formula (or object that can be coerced to one) with no response variable (optional). If omitted, all variables in "data" will be included.
...
Additional arguments to pass to model function
object
Fitted model

See Also

kmeans

Examples

Run this code
# Standard kmeans
kmeans(iris, centers = 3)

# formula interface can be used to select certain variables
kmeans(iris, ~ Petal.Width + Sepal.Width, centers = 3)

#... or create new variables. Consider non-linear example:

# Create data for two circles
x <- matrix(rnorm(300),nc=2)
y <- x/sqrt(rowSums(x^2))
d <- data.frame(rbind(y, y*5))
plot(d)

fit <- kmeans(d, centers = 2)
plot(d, col = predict(fit, d))

fit <- kmeans(d, ~ X1 + X2 + I(X1^2 + X2^2), centers = 2)
plot(d, col = predict(fit, d))

Run the code above in your browser using DataLab