[R] Replacing an element in a vector

Ivan Calandra ivan.calandra at uni-hamburg.de
Mon Feb 28 14:42:45 CET 2011


Dear Vincy,

Try this:
C[is.nan(C)] <- 0

HTH,
Ivan

Le 2/28/2011 13:10, Vincy Pyne a écrit :
> Dear R helpers
>
> I seem to have one trivial problem but can't find solution to it.
>
> Suppose I have following input.
>
> A = c(1,  3,  0,  5,  8)                  # 3rd element is 0
> B = c(100, 30,  0,  25,  40)          # 3rd element is 0
>
> C = A/B
>
>> C
> [1] 0.01 0.10  NaN 0.20 0.20
>
> Obviously, I can't divide 0/0 and hence NaN. My problem is how to replace this NaN say by 0.
>
> So that I can have C as
>
> C = c(0.01, 0.10, 0, 0.20, 0.20)
>
> I tried the replace command but can't get rid of NaN.
>
> Kindly guide.
>
> Vincy
>
>
>
> 	[[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.

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