[R] Indexing a matrix within loops

michael watson (IAH-C) michael.watson at bbsrc.ac.uk
Sat Mar 13 19:59:15 CET 2010

aggregate(x[,1:2], by=list(factor=x$factor.level), mean)
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Josh B [joshb41 at yahoo.com]
Sent: 13 March 2010 18:50
To: R Help
Subject: [R] Indexing a matrix within loops


I was hoping someone could help me with the following problem. Consider this toy example. For the input dataset there are four individuals (rows "indv.1" through "indv.4"), measured for two different variables (columns "var.1" and "var.2") at two different levels of a factor (column "factor.level"). I want to calculate a matrix that has the average values for each trait (rows "var1.avg" and "var2.avg") separated by each level of the factor (columns "factor.a" and "factor.b").

Please try my code below. I'm stuck at the part where I write the averages for each trait to the output matrix (see "y[j,i] <- mean(d[,j])" below). I think my code is wrong at this step, but I don't know how to do it right. To put it another way, why does my final product (the matrix called "y") have no values in it?

My real dataset has many more variables (210) and many more factors
(10) than this toy example, so any coding strategy must scale up well. Thanks very much in advance!

x <- read.table(textConnection("var.1 var.2 factor.level
indv.1 3 4 a
indv.2 8 7 a
indv.3 4 3 b
indv.4 0 9 b"), header = TRUE)

y <- matrix(nrow = 2, ncol = 2)
colnames(y) <- c("factor.a", "factor.b")
rownames(y) <- c("var1.avg", "var2.avg")

for(i in 1:2) {
    by(x, x$factor.level, function(d) {
        for (j in 1:2) {
            y[j,i] <- mean(d[,j])


        [[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list