matrix looping accessing previous column

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

matrix looping accessing previous column

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

Re: matrix looping accessing previous column

Henrique Dallazuanna
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: matrix looping accessing previous column

R_novice
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!
Reply | Threaded
Open this post in threaded view
|

Re: matrix looping accessing previous column

Henrique Dallazuanna
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: matrix looping accessing previous column

R_novice
That just converts my values in V3 to NA....
Reply | Threaded
Open this post in threaded view
|

Re: matrix looping accessing previous column

R_novice
Using which() improves my code, but now I'm receiving some data.frame error and still does not convert the values to negative.

#TAKE PART OF MATRIX
CEM1_PARTIAL <- CEM1[1:10,1:10]
#CEM1_PARTIAL=apply(CEM1_PARTIAL,2,as.character)

#CEM1_PARTIAL <- as.numeric(as.character(CEM1_PARTIAL))
#CEM1_PARTIAL[which(CEM1_PARTIAL$V4 == "DOWN") - 1] <- CEM1_PARTIAL$V3[which(CEM1_PARTIAL$V4 == "DOWN") - 1] * -1

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

Error in data.frame(value, row.names = rn, check.names = FALSE, check.rows = FALSE) :
  row names supplied are of the wrong length