# matrix looping accessing previous column

6 messages
Open this post in threaded view
|

## matrix looping accessing previous column

 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
Open this post in threaded view
|

## Re: matrix looping accessing previous column

 Try this: > x      V1 V2  V3   V4 1 PROBE  1 2.5   UP 2 PROBE  2 1.0   UP 3 PROBE  3 1.4 DOWN 4 PROBE  4 2.0   UP 5 PROBE  5 1.3 DOWN x\$V3[which(x\$V4 == "DOWN") - 1] <- x\$V3[which(x\$V4 == "DOWN") - 1] * -1 On Mon, Dec 27, 2010 at 4:11 PM, R_novice <[hidden email]> wrote: > > 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. > > ______________________________________________ > [hidden email] 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: matrix looping accessing previous column

 Thank you for the quick response :-). I've applied your suggestion to my code, but I still receive an error: > CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") - 1] <- CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") - 1] * -1 Warning message: In Ops.factor(CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") -  :   * not meaningful for factors I would like to perform this evaluation in a loop b/c my data matrix actually contains 109 columns, and 32,000 rows. More suggestions are appreciated! Thanks!
Open this post in threaded view
|

## Re: matrix looping accessing previous column

 Convert your column to numeric: CEM1_PARTIAL\$V3 <- as.numeric(as.character(CEM1_PARTIAL\$V3)) On Mon, Dec 27, 2010 at 4:28 PM, R_novice <[hidden email]> wrote: > > Thank you for the quick response :-). I've applied your suggestion to my > code, but I still receive an error: > > > CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") - 1] <- > > CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") - 1] * -1 > Warning message: > In Ops.factor(CEM1_PARTIAL\$V3[which(CEM1_PARTIAL\$V4 == "DOWN") -  : >  * not meaningful for factors > > I would like to perform this evaluation in a loop b/c my data matrix > actually contains 109 columns, and 32,000 rows. > > More suggestions are appreciated! > > Thanks! > -- > View this message in context: > http://r.789695.n4.nabble.com/matrix-looping-accessing-previous-column-tp3165308p3165325.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.