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

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Fri, 31 Oct 2008 12:03:44 -0500

Hello -

Diogo André Alagador 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 cannot reproduce this:

a <- matrix(sample(-10:10, 100, replace = TRUE), nrow = 10) ifelse(a > 0 , a, NA)

gives me a 10 x 10 matrix of positive values and NA values, with 10 x 10 being the original dimensions of a.

rowMeans(ifelse(a > 0 , a, NA), na.rm = TRUE)

gives me what you'd like for positive values, I think?

>
> 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.



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. Received on Fri 31 Oct 2008 - 17:11:37 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 31 Oct 2008 - 17:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive