dat.ml <- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
cluster = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
x1 = c(4, 2, 5, 6, 3, 4, 1, 3, 4),
x2 = c(2, 5, 3, 1, 2, 7, 4, 5, 3))
# Compute cluster means and expand to match the input x
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster)
# Compute standard deviation for each cluster and expand to match the input x
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster, fun = "sd")
# Compute cluster means without expanding the vector
cluster.scores(dat.ml$x1, cluster = dat.ml$cluster, expand = FALSE)
# Compute cluster means and attach to 'dat.ml'
dat.ml <- cbind(dat.ml,
cluster.scores(dat.ml[, c("x1", "x2")], cluster = dat.ml$cluster))
Run the code above in your browser using DataLab