R by example


Vectors, lists, matrices, data frames Sorting Prices and returns
Writing functions Amazing R vector notation Amazing R indexing notation
Making latex tabular objects Associative arrays / hashes Matrix notation (portfolio computations in financial economics)
Handling missing data

Reading files

Reading a file with a few columns of numbers, and look at what is there. Reading a file involving dates Reading in a file made by CMIE's Business Beacon program
Reading and writing both ascii files and binary files. Also, measure speed of these. Sending an R data object to someone else, either in email or as a binary file. Make a "zoo" object, for handling time-series data.
Exporting and importing data. Reading .gz .bz2 files and URLs Directly reading Microsoft Excel files


A grid of multiple pictures on one screen Making PDF files that go into books/papers A histogram with tails in red
z=f(x,y) using contour lines and colours Show recessions using filled colour in a macro time-series plot Plotting two series on one graph, one with a left y axis and another with a right y axis.

Probability and statistics

Tables, joint and marginal distributions `Moving window' standard deviation Quartiles/deciles tables/graphs.
[Requires this data file.]
Distribution of sample mean and sample median The bootstrap
[Notes on boot()]
Doing MLE with your own likelihood function
[Notes on MLE]
The strange Cauchy distribution An example of simulation-based inference Four standard operations with standard distributions
Two CDFs and a two-sample Kolmogorov-Smirnoff test Simulation to measure size and power of a test


Doing OLS Dummy variables in regression Generate latex tables of OLS results
`Least squares dummy variable' (LSDV) or `fixed effects' model Estimate beta of Sun Microsystems using data from Yahoo finance : Elaborate version, Terse version. Nonlinear regression
Standard tests Using orthogonal polynomials A function that takes a model specification as an argument

Time-series analysis

ARMA estimation, diagnostics, forecasting

All these examples in one tarfile.

Outright non-working code is unlikely, though occasionally my fingers fumble or code-rot occurs. More importantly, I am not an R guru. So I will always be happy if you alert me to mistakes or improvements. Please do also send me requests for things that ought to be on this page and aren't (ideally with the code!).

Other useful materials

Suggestions for learning R

The R project is at http://www.r-project.org : In particular, see the `other docs' there.

Over and above the strong set of functions that you get in `off the shelf' R, there is a concept like CPAN (of the perl world) or CTAN (of the tex world), where there is a large, well-organised collection of 3rd party software, written by people all over the world. This is called `CRAN' for Comprehensive R Archive Network. You will be amazed at the quality and comprehensiveness of the code out there.

The dynamism of R and of the surrounding 3rd party packages has thrown up the need for a newsletter, R News. You should make it a point to look hard at back issues. The standard online documents associated with R tend to be reference manuals targeting someone who already knows quite a bit. The articles in R News are very valuable in taking you from scratch to understanding R. As an example, you can certainly learn using the online documents on the boot() package, by saying:


But you will learn a lot more by reading the article Resampling Methods in R: The boot package by Angelo J. Canty, which appeared in the December 2002 issue of R News.

Ajay Shah, 2005