[R] translate grouped data to their centroid
Michael Friendly
friendly at yorku.ca
Tue Jan 1 20:50:15 CET 2013
Given a data set with a group factor, I want to translate the numeric
variables to their
centroid, by subtracting out the group means (adding back the grand means).
The following gives what I want, but there must be an easier way using
sweep or
apply or some such.
iris2 <- iris[,c(1,2,5)]
means <- colMeans(iris2[,1:2])
pooled <- lm(cbind(Sepal.Length, Sepal.Width) ~ Species,
data=iris2)$residuals
pooled[,1] <- pooled[,1] + means[1]
pooled[,2] <- pooled[,2] + means[2]
pooled <- as.data.frame(pooled)
pooled$Species <- iris2$Species
--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street Web: http://www.datavis.ca
Toronto, ONT M3J 1P3 CANADA
More information about the R-help
mailing list