R Syntax

R syntax covers how statements, assignments, and function calls are written so the interpreter can understand your code.

Statements in R

An R program is a sequence of statements. Each statement is usually written on its own line, and R reads the line, evaluates it, and moves to the next one. A semicolon can separate two statements placed on the same line, but this is rarely used in practice.

Example

x <- 10
y <- 20
print(x + y)

Assignment

Values are stored in variables using the assignment arrow <-. R also accepts = for assignment in most contexts, and even -> to assign left-to-right, though <- is the idiomatic style used throughout the R community.

OperatorExampleMeaning
<-x <- 5Assigns 5 to x (preferred style)
=x = 5Assigns 5 to x (also valid)
->5 -> xAssigns 5 to x, written right-to-left

Case Sensitivity

R is case-sensitive, so total, Total, and TOTAL are three distinct variables. This applies to function names too — Print and print are not the same thing, and only the lowercase print exists in base R.

Function Calls

Functions are called by writing their name followed by parentheses containing zero or more arguments, such as sum(1, 2, 3) or print("hi"). Arguments can be positional or named, and named arguments can be supplied in any order.

Example

seq(from = 1, to = 10, by = 2)
Note: R ignores extra whitespace and blank lines between statements, so you can format your code with spacing and indentation to make it easier to read without changing what it does.

Exercise: R Syntax

Which function is the standard way to print output to the R console?