[R] Conditional Operation in Matrix
Ivan Calandra
ivan.calandra at uni-hamburg.de
Wed Nov 24 16:05:20 CET 2010
Hi,
Would that work for you?
d <- a-b
d[a==0 | b==0] <- 0
The output is as the one expected
HTH,
Ivan
Le 11/24/2010 15:54, Romildo Martins a écrit :
> Hello,
>
> I would like to perform the operation a - b, only for the non-zero values.
>
> Thanks a lot!
>
> RMB
>
>
>> a
> 0 0 0 0 4 0 0 0
> 0 0 0 0 0 3 0 0
> 0 0 0 0 0 0 0 3
> 0 0 0 0 0 0 4 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
>
>> b
> 0 4 4 4 4 4 4 4
> 0 0 4 4 4 4 4 4
> 0 0 0 4 4 4 4 4
> 0 0 0 0 4 4 4 4
> 0 0 0 0 0 5 5 5
> 0 0 0 0 0 0 5 5
> 0 0 0 0 0 0 0 5
> 0 0 0 0 0 0 0 0
>
>> a-b (expected)
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 -1 0 0
> 0 0 0 0 0 0 0 -1
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0
>
> ______________________________________________
> 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.
>
--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
More information about the R-help
mailing list