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

Greg Snow Greg.Snow at imail.org
Fri Oct 31 18:02:15 CET 2008


The ifelse function sometimes has unwanted side effects.  Your problem can easily be done with simple subsetting.  Try:

> pos <- neg <- A
> pos[ pos <= 0 ] <- NA
> neg[ neg >= 0 ] <- NA

# remove the ='s if you don't want strict positive/negative

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Diogo André Alagador
> Sent: Friday, October 31, 2008 8:59 AM
> To: r-help at r-project.org
> Subject: [R] [ifelse] how to maintain a value from original matrix
> without probs?
>
> 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