Hi, This would be a lot easier to answer if you provided a small test dataset using dput(); I have trouble working through code if I have no idea what the input data looks like.
One thing I did spot is a problem in your indexing: j is a scalar, so indexing j[2] won't work. Presumably you got the brackets wrong, and want cells[j][1] and cells[j][2], but again, without knowing what cells is, I can't say for certain. These are the lines I'm referring to: Row<-BasinPollutant[cells[j[1]]] Column<-BasinPollutant[cells[j[2]]] Sarah On Mon, Mar 24, 2014 at 10:02 AM, Vermeulen, Lucie <lucie.vermeu...@wur.nl> wrote: > I am trying to write an R script to do pollution routing in world rivers, and > need some help on selecting matrix cell coordinates and applying these to > other matrices of the same dimension. > > My data: I have several matrices corresponding to hydrological parameters of > world rivers on a half degree grid (360 rows, 720 columns). These matrices > represent flow accumulation (how many cells flow into this cell), flow > direction (which of the 8 surrounding cells does the load of certain cell > flow to) and pollutant load. > > My idea: compute pollutant load in each grid cell from the start to the end > of a river. I can base this on flow accumulation (low to high). However, each > river basin can have multiple cells with the same flow accumulation value. > > The problem: I need to select all matrix cells of each value of flow > accumulation (low to high), find their coordinates (row,column), and transfer > the corresponding pollutant load to the correct adjacent cell using the flow > direction matrix. I have tried various ways, but selecting the coordinates of > the correct cells and applying these to another matrix I cannot get to work. > > I will give an example of what I have tried, using two for loops on one > single river basin. In this example, a flow direction value of 1 means that > the pollutant load needs to be transferred to the adjacent cell to the right > (row is the same, column +1): > > BasinFlowAccumulation <-FlowAccumulation[Basin] > > BasinFlowAccumulationMaximum <- max(BasinFlowAccumulation) > > BasinFlowDirection <-FlowDirection[Basin] > > BasinPollutant <-Pollutant[Basin] > > > b<-0 > > for(i in 0:BasinFlowAccumulationMaximum){ > > cells.index<-which(BasinFlowAccumulation[]==b, arr.ind=TRUE) > > for (j in 1:length(cells.index)){ > print(BasinFlowDirection[cells[j]]) > Row<-BasinPollutant[cells[j[1]]] > Column<-BasinPollutant[cells[j[2]]] > ifelse(BasinFlowDirection[cells.index[j]]==1, > BasinPollutant[Row,(Column+1)]<- BasinPollutant [Row,(Column+1)]+ > BasinPollutant [Row,Column] > > } > b<-b+1 > > } > -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ 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.