;
or newline separate commands.#
to end of line).x<-y
(assignment) is parsed differently than x < -y
(comparison)!b
in a
: a <- b; b -> a; assign("a", b); a = b;
. There are subtle differences and some authorities prefer <-
for assignment but I'm increasingly convinced that they are wrong; =
is always safer. R doesn't allow expressions of the form if (a = testSomething())
but does allow if (a <- testSomething())
. To assign to globals, though, use <<-
.$
acts kind of like the .
scope operator in C-like languages, at least for data frames. If you'd write struct.instance_variable
in C, you'd maybe write frame$column.variable
in R.for
loops. The syntax is vaguely Pythonic: for(i in <sequence>) { do something; }
. You may be tempted to use the sequence operator, :
, which is akin to range()
in Python, to generate a list of integers to iterate over. Two cautions here. First, this is rarely the R idiom to use; as in MATLAB, vector operations are usually faster and harder to screw up than iteration (see important caveat in "vector operations" below). Reference the third chapter of the R inferno for advice on vectorizing. Second, if you do something like i in 1:foo
, the wrong thing will happen if foo
ever holds the value 0. 1:0
is the sequence (1, 0), since the :
operator can and will count backwards. Always check whether foo
is zero before you run your loop if you use :
. If you're iterating over the indices of a sequence, always use seq_along(x)
in preference to 1:length(x)
.source('filename.R')
.library(foo)
or library('foo')
or require(foo)
or require('foo')
. library()
is actually more stringent, and dies on an error if the library can't be found. The return value of require()
is TRUE
if loading the library was successful but failure to load the library is a warning and isn't fatal.option()
calls in their .Rprofile
file. This is a way to change the default behavior of some functions and it's an awful idea, because it will change the behavior of anybody else's code (including libraries) that depends on the default settings, in a way that's really hard to debug when you forget about it.The R interpreter's built-in help feature is the only place I can consistently find documentation on anything. Try: ?function_name
or ??search_term
.
Because even R's name is stupid, it's really hard to google R things in a useful way. There is a tool called RSeek that pretends to help but it just returns different useless results. Sorry. Welcome to R! Your life is now hell.
The Hyperpolyglot page comparing MATLAB, R, and NumPy syntax is helpful. John D. Cook's R programming for those coming from other languages page is brief but useful. The R inferno offers complementary advice but blames the victim.
R messiah Hadley Wickham has a very useful wiki-book on advanced R development. The vocabulary appendix is an excellent list of things to learn.
Stack Overflow and the R-Help mailing list are good places to ask for help. There is offputtingly dense advice on asking good questions.