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

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

Variables

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

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)
> 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")
            on.exit(close(file))
        }
        else {
            file <- file(file, ifelse(append, "a", "w"))
            on.exit(close(file))
        }
    .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:

if(condition)
{
	doSth()
}