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.