[R] 0* log(0) should be zero but NaN [Broadcast]

Liaw, Andy andy_liaw at merck.com
Wed Jul 12 02:08:55 CEST 2006


Try:

R> (cal <- prob * log(ifelse(prob == 0, 1, prob), base=2))
[1] -0.5000000 -0.5287712 -0.5210897 -0.3321928  0.0000000

Andy 

From: Taka Matzmoto
> 
> Dear R-users
> 
> >prob <- c(0.5,0.4,0.3,0.1,0.0)
> >cal <- prob * log(prob,base=2)
> >cal
> [1] -0.5000000 -0.5287712 -0.5210897 -0.3321928        NaN
> 
> Is there any way to change NaN to zero ?
> 
> I did come up with this by applying Ripley's relpy to my 
> previous question
> 
> cal <-prob*log(pmax(prob,0.00000001),base=2)
> 
> Any suggestion ?
> 
> Thank you
> 
> Taka
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>



More information about the R-help mailing list