Does this do what you want? Another way of doing matrix addressing: > dat[cbind(year,region)] [1] 1 5 9 10 18 3 4 8 16 17 2 6 7 15 19
On 10/23/07, Daniel O'Shea <[EMAIL PROTECTED]> wrote: > I am trying to distribute the entries of a data frame (dat) to a vector > (water) based on the values of two other vectors (region and year). region > is also the columns and year the rows of the data frame (dat). I can write a > bunch of ifelse statements or I created the for statement below - but there > must be a simpler way?? The for statement probably won't work if I am > distributing a non numeric factor. Is there a built in function that does > this? > > Dan O'Shea > > > ##example data > dat<-matrix(c(1,2,3,4,5,6,7,8,9,10,15,16,17,18,19),nrow=3,ncol=5) > colnames(dat)<-1:5 > rownames(dat)<-1:3 > region<-rep(1:5,3) > year<-rep(1:3,5) > > > ####distributes the data frame values based on column and row identity, but > ###then must perform the apply statement to create a single vector. > water<-vector() > for(i in 1:5){ > for(j in 1:3){ > water.b<-ifelse(region==region[i]&year==year[j],dat[j,i],0) > water<-cbind(water,water.b) > }} > waterlevel<-apply(water,1,sum) > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem 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.