Monday, July 11, 2011

Expanding your language patterns: R

R (“GNU S”) is  a sweet language and environment for statistical computing and graphics. R is similar to the award-winning S system, which was developed at Bell Laboratories by John Chambers et al. It is also very similar to my first taught language APL/360 except it does not require special characters.


You can install it from, there is a Windows version which comes with it’s own UI.


As simple example.  You want to calculate   1/1 +1/2+ …. 1/1000.  A verbose solution may be:


> a1 <- 1:1000
> sum(1/a1)
[1] 7.485471


A one-liner (which APL is infamous for) would be:

> sum(1/1:1000)
[1] 7.485471


You can also do some really interesting stuff, like finding the sum of all the square roots from –1 to -45


a1 <- -1:-45
> a2 <- a1+ 0i
> sum(sqrt(a2))
[1] 0+204.3985i


Yes, complex number support is native!


Often in programming you end up with a hyped-focused on a particular language pattern such as compiled object orientated in-fix languages. Languages such as R or F# or Prolog may be incomprehensible. If you become familiar with different ways of doing things, you may find that your solutions become more elegant. Often you will look at a problem and see a simple solution in another language pattern; this may result in an atypical elegant solution. Thinking outside of the silo.


Download it and walk through some tutorials.  You may find R’s way of doing things very refreshing!

No comments:

Post a Comment