[R] vector is not assigned correctly in for loop

R. Michael Weylandt michael.weylandt at gmail.com
Sat Oct 6 20:48:29 CEST 2012


On Sat, Oct 6, 2012 at 3:23 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> But the OP should not be doing this **at all.** He apparently has not
> bothered to read the Intro to R tutorial as he appears not to know
> about vectorized calculations.
>
> -- Bert
>

I don't really think that's relevant or constructive here. Yes, it
would be more idiomatic, as I and Rui already noted, to vectorize
_this_ trivial example, but it obviously is just a minimal example. If
the OP really wanted the result 0.02, 0.04, etc., and vectorization
really was the issue at hand, we would have just directed him to
seq(0.02, 1.00, 0.02) and that would have been that.

The meat of the question -- which I feel the OP nicely isolated -- was
the eternally surprising behavior of floating point numbers,
specifically in regards to vector subsetting. It's a fact of life that
many of us have seen before and know to deal with, but it's surprising
and profoundly counterintuitive for beginning programmers. So much so,
in fact, that the Python folks none too recently considered changing
the language so that non-integer literals would use infinite precision
(or at least accurate decimal implementations). It didn't really get
off the ground, but it was a serious consideration by several
intelligent people.

Vectorization is orthogonal to all that and rather than berating the
OP for his question and asserting that he "hasn't bothered," I would
re-commend him for a well posed and deep question, though perhaps he
didn't know quite how deep this particular rabbit hole actually goes.
It shows, to me, strong evidence of someone taking the time to isolate
the problem from a larger codebase before asking the list, and I
appreciate that fully.

Michael




More information about the R-help mailing list