R/print.R

Defines functions print.kMeans

Documented in print.kMeans

#' Printing results of k-means clustering
#'
#' \code{print} method for class \code{"kMeans"}.
#'
#' @param x an object of class \code{"kMeans"}.
#' @param ... further arguments passed to or from other methods.
#'
#' @return The method prints the group sizes, the centroids, the cluster
#'  allocations and the number of iterations that result from calling
#'   \code{kMeansLloyd}.
#' @export
#' @examples
#' # create example data set
#' X <- rbind(matrix(rnorm(50, sd = 0.5), ncol = 2),
#' matrix(rnorm(50, mean = 1, sd = 0.5), ncol = 2))
#'
#' # perform k-means algorithm
#' result <- kMeansLloyd(x = X, centroids = 2, nStart = 2)
#'
#' # print result
#' print(result)
#' @seealso \code{\link{kMeansLloyd}}
print.kMeans <- function(x, ...) {
  cat("K-means clustering with ", length(x$groupSizes), " clusters of sizes ",
      paste(x$groupSizes, collapse = ", "), "\n", sep = "")
  cat("\nCentroids:\n")
  print(x$centroids)
  cat("\nClustering vector:\n")
  print(x$cluster)
  cat("\nNumber of iterations: ", x$iterations, "\n")
  cat("\nWithin Sum of Squares:", x$withinSS, "\n", sep = "  ")
  cat("\nSum Within Sum of Squares:", x$withinTot, "\n")
}
heiligerl/kMeans_Rpackage documentation built on Aug. 16, 2020, 4:04 p.m.