[R] Perl vs. R

Douglas Bates bates at stat.wisc.edu
Tue Jun 11 18:14:25 CEST 2002


John Day <jday at csihq.com> writes:

> I am being told that R can process text files and strings as well as
> Perl (and is certainly more elegant).

"as well as" is in the eye of the beholder.  Perl is very highly tuned
to manipulating text files.  One story of how the name perl came about
is as an acronym for "Practical Extraction and Report Language".

R is an environment for statistical computing and graphics.  Although
there are pattern matching and text substitution functions in R, it is
not well suited to writing "one-off" text transformation programs.
You will find that starting R probably takes longer than the execution
of the perl program.

Rather than trying to take a simple benchmark and see how R performs
on it, it would be better to learn about the language and see if it
fulfills a real need for you.  I would suggest starting with Venables
and Ripley's "Modern Applied Statistics with S-PLUS (3rd ed)" or the
eagerly-awaited fourth edition of that book slated for publication
this summer.

Having said all this, I believe your perl program can be coded in R as
something like

  df <- read.table('infile', header = FALSE, sep = '\t', col = c('a', 'b'))
  write.table('outfile', df[order(df$b), c('b', 'a')])

although I think it would be better for you to describe what the task
is rather than providing perl code to accomplish the task.  I long ago
gave up reading other people's perl code and trying to make sense of
it.  (In the Python community there is a saying that "Hell is reading
other people's Perl code".)

> Being an R neophyte I need a little boost to get started. I have a
> little benchmark program in Perl that reads a delimited file, creates
> an inverted table and spits the file out again in key sorted order.
> 
> 
> It's just a few lines of Perl (see below). Can someone write the
> equivalent in R? The benchmark and associated files are available
> from: http://www.lib.uchicago.edu/keith/crisis/benchmarks/invert/
> 
> 
> You'll note on this page that Perl runs the benchmark in 3.5
> secs. That was in 1997. My 5.6.1 version of Perl runs it in 0.18 secs
> now, on my 600Mhz Linux platform. Wondering how fast R will be in
> comparison.
> 
> 
> Thanks,
> John Day
> 
> FYI, here's the Perl source:
> 
> #!/local/bin/perl
> # invert benchmark in Perl
> # see <url:http://www.lib.uchicago.edu/keith/crisis/benchmarks/invert/
> # Keith Waclena <k-waclena at uchicago.edu>
> 
> while (<STDIN>) {
>      chop;
>      ($a, $b) = split(/\t/);
>      $B{$b} .= "\t$a";       # gotta lose leading tab later...
> }
> 
> foreach $b (sort keys %B) {
>      # lose the leading tab with substr...
>      print "$b\t" . join("\t", sort(split(/\t/, substr($B{$b}, 1)))) . "\n";
> }
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list