tags:

views:

1631

answers:

15
+16  Q: 

Sample Code for R?

Does anyone know a good online resource for example of R code?

The programs do not have to be written for illustrative purposes, I am really just looking for some places where a bunch of R code has been written to give me a sense of the syntax and capabilities of the language?

Edit: I have read the basic documentation on the main site, but was wondering if there was some code samples or even programs that show how R is used by different people.

+17  A: 

Why not look at www.r-project.org under documentation and read at least the introduction? The language is sufficiently different from what you're used to that just looking at code samples won't be enough for you to pick it up. (At least, not beyond basic calculator-like functionality.)

If you want to look a bit deeper, you might want to look at CRAN: an online collection of R modules with source code: cran.r-project.org

zvrba
I have done that, just realised I should mention that. :)
kaybenleroll
Then you might want to look into CRAN: an online collection of R modules, with source code: http://cran.r-project.org/
zvrba
+3  A: 

As you probably know, R and S are pretty similar (apart from the cost!).

I use to use both, and I highly recommend S Poetry.

I can also highly recommend the M.J. Crawley book, and the shorter Venables & Ripley one.

Galwegian
A: 

Steve McIntyre at http://www.climateaudit.org/ is a big fan of R and often posts working code.

There is a scripts category, and the Statistics and R lists some other resources

Will Dean
What the heck...This blog is just a bunch of climate data. I thought I was going to actually benefit from this accepted-answer.
Jonathan Sampson
There is R code at that site, you just have to search a bit for it.
Boden
@Boden, which makes it a pathetic 'answer.' Might as well link to Google from now on for every question.
Jonathan Sampson
lol ?! ... why is this answer accepted ? :S
n00ki3
I've added a few more-direct links to R related stuff, which wasn't immediately obvious to find..
dbr
What an obnoxious bunch of people seem to have turned-up to savage my 9month-old answer! The questioner asked for examples of "how 'R' is used by different people", and I gave one, which he seemed to like...Funny enough, then next answer down *doesn't* answer the actual question, but is a more popular (not) answer! Strange how these things turn out. :-)
Will Dean
Probably because of this thread: http://meta.stackoverflow.com/questions/8584/question-with-irrelevant-answer-accepted-and-group-gaming. I don't think this answer deserves to be chosen, nor downvoted to this extent. The person who chose this answer deserves the downvotes more, imho.
nagul
As the guy who accepted the answer, I can honestly say there was no gaming. It seemed to be the best answer I got at the time, but it was a while ago, so I don't really remember.
kaybenleroll
The link to the scripts category redirects you to a single post ("Category Added" - http://climateaudit.org/2005/08/30/category-added/). This link worked best for me: http://climateaudit.org/category/scripts/
obvio171
+9  A: 

How about CRAN? You've got over a thousand packages of code to choose from.

hadley
+8  A: 

The simplest way of seeing code, is to

  1. install R
  2. type "help.start()" or look at online documentation, to get names of functions
  3. type the function name at the prompt

This will print the source code right at the prompt, and illustrate all manner of odd and interesting syntax corners.

Gregg Lind
Re your point 3: this shows the 'parsed' R code which has all comments stripped. Good for a quick look, but the source packages may well be more informative.
Dirk Eddelbuettel
Of course they're more informative! This method also fail for code that calls out to C programs. But it's hard to beat for speed of access, and to snoop around to find out some arcane detail.
Gregg Lind
+9  A: 

The Rosetta Code project shows R compared to other languages.

Gregg Lind
+8  A: 

I just found this question and thought I would add a few resources to it. I really like the Quick-R site:

http://www.statmethods.net/

Muenchen has written a book about using R if you come from SAS or SPSS. Originally it was an 80 page online doc that Springer encouraged him to make a 400+ page book out of. The original short form as well as the book are here:

http://rforsasandspssusers.com/

You've probably already seen these, but worth listing: http://cran.r-project.org/doc/manuals/R-intro.pdf http://cran.r-project.org/doc/contrib/Owen-TheRGuide.pdf http://cran.r-project.org/doc/contrib/Kuhnert+Venables-R_Course_Notes.zip

I don't want to sound like a trite RTFM guy, but the help files generally have great short snips of working code as examples. I'm no R pro so I end up having to deconstruct the examples to understand them. That process, while tedious, is really useful.

Good luck!

EDIT: well I hesitated to be self linking (it feels a bit masturbatory) but here's my own list of R resources with descriptions and comments on each: http://www.cerebralmastication.com/?page_id=62

JD Long
+4  A: 

It is hard to google r, because of it being too short a name. Try http://rseek.org/, which provides an r-customized Google search instead. Search on examples, code in repositories, etc.

Chang Chung
+3  A: 

Some simple examples can be found at Mathesaurus - if you know e.g. Python or Matlab, look at the respective comparison charts to find the R idioms that correspond to your familiar idioms in the other language.

Jouni K. Seppänen
+5  A: 
amoeba
+4  A: 

http://had.co.nz/ggplot2/ has a lot of graphics with example code. And you only need one package to create almost every graph you need.

Thierry
+6  A: 

The Learning R blog has a lot of good examples. Lately, the author has been doing a visualization series, comparing Lattice and ggplot2.

ars
+3  A: 

There is also the R Wiki which is slowly growing.

Dirk Eddelbuettel
+2  A: 

here are links to the R project group on Linkedin. I put together this list of links and a lot of people have found it useful (some have also made very useful additions)

harshsinghal
+1  A: 

Use Google Code Search with command "lang:r" and your keyword(s)

waanders