[R] matrix looping accessing previous column

R_novice kasojim at battelle.org
Mon Dec 27 19:11:03 CET 2010


Hi,

I have a matrix with numbers and character. I want to evaluate each cell and
change the value of the cell before it depending on the evaluation. My
evaluation: if a cell had the word "down" change the cell preceding it to a
negative number by multiplying that value by a -1. I am have trouble going
back on clumn to make this change. I'm receiving an error on the j-1 part,
and the "down" cells are converted to 'NA'.
A sample of my data matrix and code are pasted below. Any suggestions would
be appreciated. Thanks!

Data:

PROBE 1     2.5     UP
PROBE 2     1        UP
PROBE 3     1.4     DOWN
PROBE 4     2.0     UP
PROBE 5     1.3     DOWN

#TAKE PART OF MATRIX
CEM1_PARTIAL <- CEM1[1:3,1:5]

for(j in 1:ncol(CEM1_PARTIAL))
{
    for(i in 1:nrow(CEM1_PARTIAL))
    {
      if(CEM1_PARTIAL[i,j] == "down")
      {CEM1_PARTIAL[i,j-1]= CEM1_PARTIAL[i,j-1] * -1;}
    }
} 

error:
Warning messages:
1: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors
2: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors
3: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors
4: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors
5: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors
6: In Ops.factor(CEM1_PARTIAL[i, j - 1], -1) :
  * not meaningful for factors

-- 
View this message in context: http://r.789695.n4.nabble.com/matrix-looping-accessing-previous-column-tp3165308p3165308.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list