To run the code paste it into an R console window.

a <- 42
A <- a * 2  # R is case sensitive
cat(A, "\n") # "84" is concatenated with "\n"
if(A>a) # true, 84 > 42
  cat(A, ">", a, "\n")
[1] 42
84 > 42


R uses <- for variable assignment.

Don't call your variables any of the following:

  • c, q, s, t, C, D, F, I, T
  • diff, length, mean, pi, range, rank, time, tree, var
  • if, function, NaN etc.

Howard Seltman provides more information about reserved terms in this "Learning R" lesson.
You can use underscores and periods in your identifiers. Google suggests tree.count for variables and DoSomething for functions.


Comments start with a # sign. Block Comments can't be be done nicely.

print and cat

print() automatically appends a new line character to the output. With cat() you have to append it manually. print() can also show more types of content, such as functions:

> print(cat)
function (..., file = "", sep = " ", fill = FALSE, labels = NULL, 
    append = FALSE) 
    if (is.character(file)) 
        if (file == "") 
            file <- stdout()
        else if (substring(file, 1L, 1L) == "|") {
            file <- pipe(substring(file, 2L), "w")
        else {
            file <- file(file, ifelse(append, "a", "w"))
    .Internal(cat(list(...), file, sep, fill, labels, append))
<environment: namespace:base>
You can get the documentation of a function from the R console if you type help(print).

if statements

If statements are quite straightforward: