[R] [ifelse] how to maintain a value from original matrix without probs?
Erik Iverson
iverson at biostat.wisc.edu
Fri Oct 31 18:03:44 CET 2008
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.
More information about the R-help
mailing list