try this: > # create indexing vector to select 3 adjacent columns > indx <- sapply(seq(1, 36, 3), seq, length = 3) > # process each row of the table > ans <- t(apply(data, 1, function(.row){ + # use indx to sum up the columns + apply(indx, 2, function(.indx){ + sum(.row[.indx]) + }) + })) > ans [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,] 18 63 108 153 198 243 189 36 81 126 171 216 [2,] 21 66 111 156 201 246 192 39 84 129 174 219 [3,] 24 69 114 159 204 249 195 42 87 132 177 222 [4,] 27 72 117 162 207 252 198 45 90 135 180 225 [5,] 30 75 120 165 210 255 102 48 93 138 183 228
On Mon, Dec 20, 2010 at 5:48 AM, Anne-Christine Mupepele <anne-chr....@web.de> wrote: > Hi, > I have the following problem: > > I have a data.frame with 36 sample sites (colums) for which I have covariates > in 3 categories: Area, Month and River. Each Area consists of 3 rivers, which > were sampled over 3 month. Now I want to fuse River 1-3 for one area in one > month. To get a data.frame with 12 colums. > I am trying to do a "for loop" (which may be a complicated solution, but I > don't see an easier way), which is not working, apparently because a[,ij] or > a[,c(i,j)] is not working as a definition of the matrix with a double > condition in the colums. > How can I make it work or what would be an easier solution? > > Thank you for your help, > Anne > > data=data.frame(matrix(1:99,nrow=5,ncol=36)) > colnames(data)=c(paste("plot",1:36)) > cov=data.frame(rep(1:3,12),c(rep("Jan",12),rep("Feb",12),rep("Mar",12)),rep(c(1,1,1,2,2,2,3,3,3,4,4,4),3)) > dimnames(cov)=list(colnames(data),c("River","Month","Area")) > > ###loop### > a=matrix(nrow=dim(data)[1],ncol=length(levels(factor(cov$Month)))*length(levels(factor(cov$Area)))) > > for(i in 1:length(levels(factor(cov$Month)))) > { > for(j in 1:length(levels(factor(cov$Area)))) > { > a[,ij]=as.numeric(rowSums(data[,factor(cov$Month)==levels(factor(cov$Month))[i]&factor(cov$Area)==levels(factor(cov$Area))[j]])) > } > } > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.