R Syntax 101: Write Your First Working Script in 10 Minutes
R syntax is how you tell R what to do. You create variables with <-, call functions with parentheses, and chain operations together. This tutorial covers every syntax rule you need to start writing real R code.
If you've installed R and RStudio, you're ready to write code. This tutorial teaches you R's core syntax — variables, operators, functions, comments, and how to combine them into a working script. Every concept includes an interactive code block you can run immediately.
Introduction
Every programming language has syntax rules — the grammar that tells the computer how to interpret your commands. R's syntax is designed for data analysis, so it has some unique features:
- The
<-arrow for assigning values (instead of=in most other languages) - 1-based indexing (counting starts at 1, not 0)
- Built-in support for vectors and statistical functions
- The pipe operator (
|>) for chaining operations
Don't worry if those sound unfamiliar. By the end of this tutorial, you'll understand all of them and have written a complete R script from scratch.
Here's what we'll cover:
- Comments — how to annotate your code
- Variables and assignment — storing values
- Data types — the kinds of values R understands
- Operators — math, comparison, and logic
- Functions — using R's built-in tools
- Writing a complete script
Comments: Annotating Your Code
Comments are notes you write for yourself (and others) inside your code. R ignores everything after a # symbol on a line.
Comments are essential for making your code readable. When you come back to a script after two weeks, comments remind you what you were thinking.
Rule of thumb: If you had to think about why you wrote a line, add a comment. If the code is self-explanatory (
x <- 10), skip the comment.
R does not have multi-line comments (no /* */ like C or Python's triple quotes). To comment out multiple lines, select them in RStudio and press Ctrl+Shift+C — it adds # to each line.
Variables and Assignment
A variable stores a value so you can use it later. In R, you create variables with the assignment operator <-:
The <- arrow means "assign the value on the right to the name on the left." So age <- 30 creates a variable called age and stores the number 30 in it.
Why <- instead of =?
You can use = for assignment in R, and it works in most cases. But <- is the R convention for three reasons:
- Clarity —
<-visually shows direction: value flows into the name - Tradition — the R community uses
<-, and consistency matters when reading others' code - Edge cases — in some contexts (function arguments),
=means something different than assignment
Use <- for assignment. The RStudio shortcut is Alt+- (Windows/Linux) or Option+- (Mac) — it types <- with spaces automatically.
Variable naming rules
| Rule | Valid | Invalid |
|---|---|---|
| Start with a letter or dot | my_var, .hidden | 1var, _var |
| Use letters, numbers, dots, underscores | data_2024, my.data | my-data, my data |
| Case-sensitive | Age ≠ age | — |
| No reserved words | my_if, data1 | if, TRUE, function |
Best practice: Use snake_case for variable names — all lowercase with underscores: student_count, avg_score, file_path. This is the tidyverse convention and makes code most readable.
Data Types: What R Stores
Every value in R has a type. The four types you'll use most often are:
You can check any value's type with class(). Most of the time, R handles types automatically — you don't need to declare types like in Java or C. R just figures it out from the value you assign.
Key insight: In R, even a single number is actually a vector of length 1. This is different from most other languages and is the reason R is so good at working with data — everything is built around collections of values.
Operators: Math, Comparison, and Logic
Arithmetic operators
R works as a calculator. Try these:
Two operators might be unfamiliar: %/% (integer division — drops the decimal) and %% (modulo — gives the remainder). These are useful for tasks like checking if a number is even (x %% 2 == 0).
Comparison operators
Comparison operators return TRUE or FALSE. They're essential for filtering data and making decisions:
The biggest trap for beginners: = is assignment, == is comparison. Writing if (x = 5) when you mean if (x == 5) is a common bug. R will actually warn you about this one.
Logical operators
Logical operators combine TRUE/FALSE values:
| Operator | Meaning | Example | Result |
|---|---|---|---|
& | AND | TRUE & FALSE | FALSE |
| | OR | TRUE | FALSE | TRUE |
! | NOT | !TRUE | FALSE |
These become crucial when you start filtering datasets: "show me all rows where age > 25 AND salary > 50000."
Functions: R's Built-In Tools
A function takes inputs, does something, and returns an output. R has thousands of built-in functions. You call a function by typing its name followed by parentheses containing the arguments:
Let's break down the anatomy of a function call:
mean(numbers)—meanis the function name,numbersis the argumentround(sd(numbers), 2)— nested call:sd(numbers)runs first, thenround()rounds the result to 2 decimal placescat("Sum:", sum(numbers), "\n")—cat()prints text,\nadds a newline
Functions with multiple arguments
Many functions accept optional arguments that change their behavior:
Notice how some arguments are named (by = 2, length.out = 5). Named arguments let you skip arguments or pass them in any order. Unnamed arguments must be in the right position.
Getting help on functions
When you don't know what arguments a function accepts, use the ? operator:
The c() Function: Creating Vectors
The c() function (short for "combine") is one of the most important functions in R. It creates vectors — ordered collections of values:
A neat trick: mean() on a logical vector gives you the proportion of TRUE values. That's because R treats TRUE as 1 and FALSE as 0. So mean(passed) = 0.8 = 80% pass rate.
Printing Output
R has several ways to display output. Here's when to use each:
| Method | Best for | Adds [1]? | Adds newline? |
|---|---|---|---|
| Variable name | Quick checks in Console | Yes | Yes |
print() | Inside functions/loops | Yes | Yes |
cat() | Clean output, combining values | No | Only with \n |
paste() | Building strings to store/use | N/A | No |
sprintf() | Formatted numbers/text | No | Only with \n |
Writing a Complete Script
Let's put everything together into a real R script. In RStudio, you'd save this as a .R file and run it line by line with Ctrl+Enter:
This script demonstrates everything you've learned: variables, vectors, arithmetic, comparison operators, functions, cat() for output, sprintf() for formatting, and even a for loop (which we'll cover in detail in a later tutorial).
Practice Exercises
Exercise 1: Variable Basics
Create variables for a product and calculate the total cost:
Click to reveal solution
Explanation: sprintf() uses format codes: %d for integers, %s for strings, %.2f for numbers with 2 decimal places. The \n adds a newline.
Exercise 2: Comparison and Logic
Determine if a student qualifies for honors:
Click to reveal solution
Explanation: The & operator requires ALL conditions to be TRUE. Student B has a good GPA and clean record, but attendance is 85% — below the 90% threshold — so the result is FALSE.
Exercise 3: Functions and Vectors
Analyze a dataset of temperatures:
Click to reveal solution
Explanation: The key insight is that (temps_f - 32) * 5/9 converts ALL temperatures at once — R automatically applies math to every element in a vector. Similarly, temps_f > 73 returns a logical vector, and sum() counts the TRUEs.
Summary
Here's a quick reference for everything you learned:
| Concept | Syntax | Example |
|---|---|---|
| Comment | # text | # This is a comment |
| Assignment | <- | x <- 42 |
| Numeric | bare number | 3.14 |
| Character | quotes | "hello" |
| Logical | TRUE/FALSE | TRUE |
| Arithmetic | + - * / ^ %% %/% | 2 + 3 * 4 |
| Comparison | == != > < >= <= | x >= 10 |
| Logical | & | ! | a > 0 & b > 0 |
| Function call | name(args) | mean(x) |
| Create vector | c() | c(1, 2, 3) |
| Print (clean) | cat() | cat("Hi\n") |
| Get help | ?name | ?mean |
FAQ
Why does R use <- instead of = for assignment?
Historical reasons — R inherited <- from the S language (1976), which was inspired by APL where a left-arrow key existed on keyboards. Today, = works for assignment in most situations, but <- is the community convention. Use <- to follow R style guides and avoid rare edge-case bugs.
What does [1] mean in R output?
It's an index label. When R prints a result, [1] means "the first element starts here." For long vectors, you'll see [1], [14], [27], etc. — telling you which position each row starts at. It's informational; you don't need to type it.
Can I use spaces around <- and operators?
Yes, and you should. x <- 10 is much more readable than x<-10. R ignores whitespace around operators. The standard style guide recommends spaces around <-, =, +, -, *, /, and all comparison operators.
What happens if I type a variable name wrong?
R throws an error: Error: object 'misspelled_name' not found. This is the most common R error for beginners. Check your spelling and capitalization — R is case-sensitive (myVar ≠ myvar).
Can I run R code without RStudio?
Yes. You can run R from the command line by typing R (starts an interactive session) or Rscript myfile.R (runs a script file). However, RStudio makes everything easier — especially viewing plots, managing files, and debugging errors.
What's Next?
You now know the fundamental syntax of R. The next tutorials dive deeper into R's core data structures:
- R Data Types — understand the six types and why they matter for data analysis
- R Vectors — master the most important data structure in R
- R Data Frames — work with tabular data (rows and columns)
Each tutorial builds on what you've learned here, and all include interactive code blocks for hands-on practice.