r - how to perform a matrix calculation (eg covariance matrix) for different levels of factor variable -
i have following simplified problem:
temp <- matrix(rnorm(900), ncol = 3) lev <- as.factor(rep(c("a", "b", "c"), each = 100)) dfr <- data.frame(lev = lev, temp = temp)
i wanted calcualte variance-covariance matrices each of 3 levels. how do efficiently?
many taking time read (and answer) question!
never mind. found solution:
lapply(split(dfr, dfr$lev), function(x) var(x[,-1]))