[R] [ifelse] how to maintain a value from original matrix without probs?

Henrik Bengtsson hb at stat.berkeley.edu
Fri Oct 31 17:58:25 CET 2008


> A <- matrix(-4:4, ncol=3)
> A
     [,1] [,2] [,3]
[1,]   -4   -1    2
[2,]   -3    0    3
[3,]   -2    1    4
> Apos <- A; Apos[A <= 0] <- NA;
> Apos
     [,1] [,2] [,3]
[1,]   NA   NA    2
[2,]   NA   NA    3
[3,]   NA    1    4
> Aneg <- A; Aneg[A >= 0] <- NA
> Aneg
     [,1] [,2] [,3]
[1,]   -4   -1   NA
[2,]   -3   NA   NA
[3,]   -2   NA   NA

/Henrik

On Fri, Oct 31, 2008 at 7:59 AM, Diogo André Alagador
<mcnda839 at mncn.csic.es> wrote:
> Dear all,
>
> I have a matrix with positive and negative values.
> >From this I would like to produce 2 matrices:
> 1st - retaining positives and putting NA in other positions
> 2nd - retaining negatives and putting NA in other positions
>
> and then apply rowMeans for both.
>
> I am trying to use the function ifelse in the exemplified form:
> ifelse(A>0,A,NA)
> but by putting A as a 2nd parameter it changes dimensions of the original
> object.
>
> I wonder if I can do this, as it seems not to difficult.
>
> thanks in advance
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list