Reputation: 11860
In the function source for stats::bw.nrd0
, there is a complicated (to me) if
statement:
> bw.nrd0
function (x)
{
if (length(x) < 2L)
stop("need at least 2 data points")
hi <- sd(x)
if (!(lo <- min(hi, IQR(x)/1.34)))
(lo <- hi) || (lo <- abs(x[1L])) || (lo <- 1)
0.9 * lo * length(x)^(-0.2)
}
<bytecode: 0x0000000010c688b0>
<environment: namespace:stats>
Is ||
to be interpreted in a special way, compared to the regular operator |
? Where/how is lo
being assigned / re-assigned? How would this be written in "long form"?
Full disclosure, I tried to translate this function to a Python function in this answer, so if you can answer this, you can also add a better answer to that question.
Upvotes: 1
Views: 320
Reputation: 11860
Thanks to some helpful comments, I now understand that this means that if lo == 0
, we assign to whichever of hi
, abs(x[1])
or 1
is non-zero first, in that order. In Python, we could write:
lo = min(hi, iqr/1.34)
lo = lo or hi or abs(x[0]) or 1
or more explicitly:
if not lo:
if hi:
lo = hi
elif abs(x[0]):
lo = abs(x[0])
else:
lo = 1
Upvotes: 3
Reputation: 40628
From the help docs:
& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.
Upvotes: 1