The shortest introduction to R

Wednesday January 1, 2014

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.

This post was originally hosted elsewhere.