Menu

R Syntax: The Basics in 5 Minutes

The core of R syntax: assignment with <-, expressions and auto-printing, function calls with named arguments, and the everything-is-a-vector rule.

This page includes runnable editors - edit, run, and see output instantly.

Expressions and Auto-Printing

R is an expression language: nearly every line you write produces a value. At the console, an expression typed on its own gets evaluated and its value printed automatically - no print() needed:

Each result is prefixed with [1] - that's R telling you the position of the first value shown, a first hint that R thinks in vectors (more below). Auto-printing has a catch worth knowing on day one: it applies to bare expressions at the top level. The same expression inside a function body or a for loop prints nothing, which is why scripts use explicit cat() or print() for output you must see.

Assignment: The Arrow

R's assignment operator is <-, an arrow pointing at the name receiving the value:

Read radius <- 5 as "radius gets 5". Yes, = also works for top-level assignment, and yes, newcomers ask why anyone types two characters instead of one. The answer is convention with teeth: every major style guide, the base R documentation, and effectively all R code you'll ever read use <- for assignment, reserving = for one specific job - naming arguments inside function calls (next section). Following the convention makes the two operations visually distinct. In RStudio, Alt+- (Option+- on macOS) types the arrow for you.

Note that an assignment doesn't auto-print - area <- 78.5 stores the value silently. That's why the snippet above needs the print().

Case Sensitivity

R is strictly case-sensitive, in names you create and names the language provides:

Two different variables. Likewise mean() is a function and Mean() doesn't exist. When R greets you with object 'x' not found or could not find function, check capitalization before anything else - it's the single most common beginner error.

Function Calls and Named Arguments

Functions are called with parentheses, arguments separated by commas. Arguments can be matched by position, by name, or a mix:

The seq() call shows R's named-argument syntax - and here is where = earns its keep: inside a call, name = value labels an argument. Named arguments free you from remembering parameter order, and they make calls self-documenting; seq(0, 100, 25) works identically but tells the reader less. The working convention: pass the first argument (usually the data) by position, name the rest.

Getting help on any function is one keystroke: ?seq at the console opens its documentation.

Everything Is a Vector

The idea that makes R feel different from other languages: there are no bare single numbers. Even 5 is a vector of length one, and operations apply element-wise to whole vectors:

No loop anywhere, yet every element was converted. In most languages that's three iterations of a for loop; in R it's arithmetic. This is vectorization, and it shapes everything - comparison operators, string functions, and math functions all work this way. It's why idiomatic R has far fewer loops than you'd expect; the full story is in vectors.

Braces and Blocks

Multi-line bodies - for if, loops, and functions - are grouped with braces {}:

Two things to notice. The condition lives in parentheses, and it's plain if, not if: or if ... then. And unlike Python, indentation carries no meaning in R - the braces define the block, the indentation is for humans. Skipping braces on one-liners is legal but a classic source of editing bugs; just always use them.

Whitespace and Semicolons

R ignores extra whitespace, and a newline ends a statement - no semicolon required:

The split across lines works because line 3 ends with +, so R knows the expression isn't finished. That's the one rule to remember when breaking long lines: break after an operator or a comma, never before, or R will treat the first line as complete. Semicolons exist only to cram two statements onto one line (a <- 1; b <- 2) - legal, discouraged, easily avoided.

Style: How R Code Is Written

Beyond the grammar, R has strong community conventions (codified in the tidyverse style guide, which most of the ecosystem follows):

  • <- for assignment, = only for arguments inside calls.
  • snake_case names: monthly_revenue, not monthlyRevenue or monthly.revenue. Dots are legal in R names but best avoided - they collide with a naming pattern R's internals use.
  • Spaces around operators (x <- a * 2, not x<-a*2) and after commas.
  • Indent with four spaces inside braces (some teams use two; pick one, stay consistent).
  • One statement per line.

None of this is enforced by the interpreter. All of it is enforced by anyone reading your code. The naming half of this story continues in variables.

What You Take Away

  • R evaluates expressions and auto-prints bare ones at the console - but not inside functions or loops.
  • Assign with <- ("gets"); use = only to name arguments in function calls.
  • R is fully case-sensitive - capitalization typos cause most early "not found" errors.
  • Everything is a vector, and operations apply element-wise: c(1, 2, 3) * 2 is 2 4 6, no loop.
  • Braces define blocks, newlines end statements, and snake_case plus spacing conventions make your code read like everyone else's.

Next up: comments - how to annotate R code, and what to do about R's missing block-comment syntax.

Frequently Asked Questions

What does <- mean in R?

<- is R's assignment operator: x <- 5 stores the value 5 under the name x. Read it as 'gets'. = also assigns at the top level, but the community convention - enforced by every major style guide - is <- for assignment and = only for naming arguments inside function calls.

Is R case sensitive?

Yes, completely. total, Total, and TOTAL are three different names, and calling Mean(x) instead of mean(x) is an error, not a warning. A 'could not find function' or 'object not found' error is very often just a capitalization typo.

Do I need semicolons in R?

No. A newline ends a statement. Semicolons are only needed to squeeze two statements onto one line (a <- 1; b <- 2), which style guides discourage anyway. Write one statement per line and never think about semicolons again.

What does it mean that R is vectorized?

R's basic operations work on whole vectors at once. c(1, 2, 3) * 2 gives 2 4 6 - the multiplication applies to every element with no loop. Most code that would be a for loop in another language is a single expression in R.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED