This morning, I started a tutorial for folks whose knowledge of R is (like mine) stuck in 2001.
Yesterday I started reading the Rcpp book, and on page 4 there’s an example using the R function replicate
, which (a) I’d never heard before, and (b) is super useful.
I mean, I often write code like this, for a bootstrap:
x <- rnorm(2500) sapply(1:1000, function(a) quantile(sample(x, replace=TRUE), c(0.025, 0.975)))
But I could just be writing
x <- rnorm(100) replicate(1000, quantile(sample(x, replace=TRUE), c(0.025, 0.975)))
“Oh, replicate
must be some new function.” Yeah, new in R version 1.8, in 2003!
I’m in serious need of some re-education (e.g., I should be using more of Hadley Wickham’s packages). Hence the beginnings of a tutorial.
Note: the title was suggested by Thomas Lumley. No connection to @hspter; it’s not really so hip. I probably should have written “geeksteR.”
15 May 2014 at 12:47 pm
I used both and am not sure why replicate is better.
15 May 2014 at 3:00 pm
Shorter and more readable.
15 May 2014 at 2:23 pm
Given that, as of a few seconds ago, there are more than 6,300 packages twixt CRAN and Bioconductor, it would be a full time job just to know the names of them, much less what they do. If there ever were a case of “there’s more than one way to do it”, R is the archetype.
15 May 2014 at 2:25 pm
Good point.
15 May 2014 at 8:36 pm
Actually I think Perl beat R to the punch as it currently has 133,389 modules on CPAN, though it’s been around longer. Whether having lots of packages is a problem is in the eyes of the beholder though as former Perl addict I got very tired of some one reading my code only to say, “you know there is a module on CPAN that does the same thing”. While I would never complain about free software that is good, I find it somewhat unnecessary to replace working code with a package under the default assumption that it is somehow “better” or “faster”. If it is, and I need that capability, then I’ll happily include it. But sometimes it winds up being a “keep up with the Jones” kind of thing.
15 May 2014 at 4:39 pm
I dislike replicate because it has a flexible (=confusing) set of “simplify” options. I like plyr::r*ply for these purposes (you get to define the return structure, and you get a progress bar thrown in !)
30 May 2014 at 12:27 pm
Re hipsteR. For programming, particularly multiple languages, you might want to include eclipse and the excellent statet (http://www.walware.de/goto/statet) plugin for R. It’s been around for many years and provides an excellent modelling, package development and general R project environment. You can check/build/debug code/packages straight from there, sweave and knitr, manage git/svn and much more.