I had reported this to R-core, but they said (without explaining) that this is not a bug in R:
During automatic processing of some data, I came across an empty data set (or similar). Anyway, the hist()
function used threw an error which looks like a syntax error to me (I'm an R beginner):
> df <- data.frame(n=c(0))
> str(df)
'data.frame': 1 obs. of 1 variable:
$ n: num 0
> hist(df$n) ### this one works!
> hist(df$n, nclass=nclass.scott) ### this does not!
Error in if (h > 0) ceiling(diff(range(x))/h) else 1L :
missing value where TRUE/FALSE needed
> df <- data.frame(n=c(0,1))
> hist(df$n, nclass=nclass.scott) ### this one works
Versions tested: 3.3.1 (linux) and 3.3.3 (Windows)
Without nclass=nclass.scott
I don't get an error. I failed to find documentation for this parameter, however; I just found that histograms with this parameter look more appealing to me. With Google I found: "nclass.scott uses Scott's choice for a normal distribution based on the estimate of the standard error, unless that is zero where it returns 1"
I'm also expecting some robustness: In automatic processing you never know how much data a particular set will have, and I would prefer a histogram with a single bar in that case. Also compare these:
> hist(numeric(0))
Error in hist.default(numeric(0)) : invalid number of 'breaks'
> hist(numeric(1))
> hist(numeric(1), nclass=nclass.scott)
Error in if (h > 0) ceiling(diff(range(x))/h) else 1L : missing value where TRUE/FALSE needed
> hist(numeric(0), nclass=nclass.scott)
Error in if (h > 0) ceiling(diff(range(x))/h) else 1L : missing value where TRUE/FALSE needed