[Rd] density(), with argument of length 1 (PR#2593)

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
Sat Mar 1 11:23:02 2003


u9801539@leonard.anu.edu.au writes:

> The following is from version 1.6.2 of R under Windows,
> or 1.6.1 under Mac OSX/X11
> 
> > density(1)
> Error in if (!(lo <- min(hi, IQR(x)/1.34))) (lo <- hi) || (lo <- abs(x[1])) ||  : 
>         missing value where logical needed
> 
> I am not sure how this should be handled.

It comes from this bit of code:

> bw.nrd0
function (x)
{
    hi <- sd(x)
    if (!(lo <- min(hi, IQR(x)/1.34)))
        (lo <- hi) || (lo <- abs(x[1])) || (lo <- 1)
    0.9 * lo * length(x)^(-0.2)
}

in which hi is NA if x has length 1. A pragmatic way out would seem to
be to change the first line to 

hi <- if (length(x) > 1) sd(x) else 0

(and I wonder why the author didn't go all the way with the ||
sneakiness and said

    (lo <- min(hi, IQR(x)/1.34)) ||
        (lo <- hi) || (lo <- abs(x[1])) || (lo <- 1)

)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907