6 comments on “Some Quirks of the R Language

  1. Re: timing of x <- x+0

    It's not an allocation thing (exactly) -- rather x is originally a vector of ints (as returned by `:`) and "x<- x + 0" entails coercion to 0 (because 0 is a double, vs 0L the integer literal for zero); for repeated work, you already have x as a double so that step gets skipped (see storage.mode() before and after). If you use x <- x + 0L, you should get roughly consistent timings each step.

    • It's a great book that I've recommended on the site several times. Every beginner with R should read it.

  2. The as.character(list(x)) example is funny but the reason is not what you think but coercion from "integer" to "numeric".

    is(1:10 + 0)

  3. I have never figured out what's strange or wrong about Ihaka's example. This is how lexical scoping works. Inside the function, the x variable is shadowed -- in this case only if the predicate evaluates to TRUE. When return is called, R first looks for an x in the function environment then, if it can't be found, looks in the calling environment. Unless I am missing something about this particular example, this is standard behavior for lexically scoped languages. Just because there is an artificial random component added into the mix doesn't mean scoping is a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

six − four =

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>