[R] matrix inf and zero's value replacement

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed May 13 08:47:48 CEST 2015



On 13.05.2015 08:04, Ragia Ibrahim wrote:
> Dear Group,
> I have the following matrix
> m
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]    0    2    1  Inf  Inf  Inf  Inf  Inf
> [2,]    1    0    2  Inf  Inf  Inf  Inf  Inf
> [3,]    2    1    0  Inf  Inf  Inf  Inf  Inf
> [4,]    3    2    1    0  Inf  Inf  Inf  Inf
> [5,]  Inf  Inf  Inf  Inf    0  Inf  Inf  Inf
> [6,]  Inf  Inf  Inf  Inf    1    0  Inf  Inf
> [7,]  Inf  Inf  Inf  Inf  Inf  Inf    0  Inf
> [8,]    1    3    2  Inf  Inf  Inf    1    0
>
> I want  all values grater than 0 = to 1 and zero other wise?
> thanks in advance
> so
> this used,
> m <-ifelse(  (m==0)||  is.infinite(m),0, 1   )
>
> but it gave me  zero   result
> replacing
> || with | ,make sense and return the matrix I was looking for.
>
> what is the difference betwen boht || an | ? when to use each ?

|| only for scalars, see the documentation.

 From your text you want something different:
m[m>0] <- 1

but your code says you want to replace Inf by 0?

Best,
Uwe Ligges



> thanks in advance
>   		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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