The shortest introduction to R

R is a curly brace language. It uses “<-” for assignment. Dots are allowed in names and don’t indicate structure. Arguments are both positional and named and can be defaulted.

subtract <- function(first.arg, second.arg = 3) {
  return(first.arg - second.arg)

Now subtract(5, 1) returns 4, as does subtract(second.arg=1, first.arg=5), and subtract(5) returns 2.

This prints the numbers 1 to 10, inclusive:

for (i in 1:10) {

Data lives in vectors – lists of values of the same type. The types are logical (TRUE or FALSE), numeric, and character (string). Vectors are put together with the function c(), which coerces and collapses things, so these two character vectors are the same:

c(1, 2, "three", 4, 5)
c(1, c(2, "three", c(4, 5)))

Just about everything in R happens element-wise on vectors. Shorter vectors are recycled (repeated) to provide enough elements when needed. So subtract(c(10, 100)) will return c(7, 97).

The heavily used data frame is essentially a list of vectors.

x <- data.frame(, 1, 2, 3, 5), col.two=6:10)
x col.two
1       1       6
2       1       7
3       2       8
4       3       9
5       5      10

You can get the first row of x with x[1, ]. You can get the second column of x with x[, 2], x[[2]] or x$col.two.

You can select elements from vectors with logicals or index numbers. These both return c(9, 10):

x$col.two[x$col.two > 8]
x$col.two[c(4, 5)]

You can get help with help() or the shortcut, ?. For example, ?read.csv or help(plot). There’s also search functionality, but I recommend using the internet instead.


This introduction is designed to maximize utility over length, and does so by leaving some things out. I recommend John Cook’s online R programming for those coming from other languages for a slightly expanded view, and Norman Matloff’s book The Art of R Programming for a proper introduction with examples and applications.

6 thoughts on “The shortest introduction to R

    • I don’t know of anything better than Matloff for really explaining the nuts and bolts step by step, building a strong foundation. A lot of R resources try to just jump right into statistical applications that leave you wondering how half the stuff is happening. So, yes, I think Matloff is quite good. Of course if you haven’t tried it yet, is another way to get a hands-on jump-start with R. Regardless of what you learn from, it’s likely that whatever specific thing you want to do with R will require some additional research, finding the right packages and syntax, etc. Get some decent foundation, and then start trying to do what you want to do! 🙂

      • @Aaron, thanks for these resources! As someone who understands programming but is new to R, your intro (and John Cook’s) were helpful, and Try R was a good first tutorial. However, I didn’t *really* start understanding data types and subsetting until I went through the first week of Coursera’s Computing for Data Analysis. @Timothy: you might find their course to be useful, and it just started up again:

        It moves fast and the “3-5 hours per week” workload is a severe underestimate for a beginner, but it’s only 4 weeks long.

        @Aaron, looking forward to your “Clean Data with R” talk tomorrow!

  1. Clean data with R – Plan Space from Outer Nine

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s