[R] try question

Mike Prager mike.prager at noaa.gov
Tue Jul 29 21:50:45 CEST 2008


"Edna Bell" <edna.bell01 at gmail.com> wrote:

> Hi still yet again!
> 
> I have the following code:
> 
> > try(log(rnorm(25)),silent=TRUE)
>  [1] -0.26396185         NaN         NaN -0.13078069 -2.44997193
> -2.15603971         NaN  0.94917495  0.07244544         NaN
> [11] -1.06341127 -0.42293099 -0.53769569  0.95134763  0.93403340
>   NaN -0.10502078         NaN  0.30283262         NaN
> [21] -0.11696872 -3.84122332         NaN         NaN -0.12808690
> Warning message:
> In log(rnorm(25)) : NaNs produced
> >
> 
> I thought that putting the "silent = TRUE" would suppress the
> warnings, please.  What should I do instead, please?

It's not a great idea to take logs of negative numbers. Better
than supressing the resulting messages, you might try something
like this:

> a <- rnorm(25)
> b <- log(ifelse(a < 0, NA, a))

which gives this:

> a
 [1] -0.04816269 -0.50745059  0.15229031  0.54735811
 [5] -0.29896853  1.81854119  0.19462259 -0.48984075
 [9]  0.63489288  1.47432484  1.15295160  0.75842227
[13]  0.07918115  1.04596643  1.31722543 -0.03614219
[17]  0.44072181  0.25358843 -0.49626405 -2.10954780
[21] -0.85815654  1.38983430  0.66592947  0.64700068
[25] -1.17829527

> b
 [1]          NA          NA -1.88196666 -0.60265201
 [5]          NA  0.59803463 -1.63669302          NA
 [9] -0.45429898  0.38820015  0.14232526 -0.27651496
[13] -2.53601706  0.04494127  0.27552758          NA
[17] -0.81934141 -1.37204269          NA          NA
[21]          NA  0.32918453 -0.40657152 -0.43540794
[25]          NA
> 

See the help page for ifelse() for more information.

-- 
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.



More information about the R-help mailing list