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 <kasojim@battelle.org> 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.
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]