Labs ICT
โญ Pro Login

R Syntax

R's syntax is pretty intuitive, especially if you've ever used a calculator. But there are a few quirks that make R, well, R. Let's walk through the fundamentals so you can start writing real commands.

R as a Calculator

At its simplest, R is a souped-up calculator. Type a math expression, hit Enter, and R prints the answer. You can do addition, subtraction, multiplication, division, exponents, and more. The console acts like a scratchpad โ€” no ceremony needed.


# Basic arithmetic
5 + 3
10 - 4
12 * 7
84 / 3
2^10
    
Try it Yourself โ†’

The print() Function

If you just type an expression, R prints it automatically. But when you're writing scripts, you often want to be explicit about what gets displayed. That's where print() comes in. It forces R to output the value of whatever you put inside the parentheses.


# Printing explicitly
print("Hi there!")
print(42)
    
Try it Yourself โ†’

Comments โ€” Talking to Yourself (and Others)

The # symbol tells R to ignore everything after it on that line. Comments are for humans. Use them to explain what your code does, leave notes, or temporarily disable code. R doesn't care about comments โ€” they're just for readability.


# This is a comment. R won't run it.

# You can put comments above a line of code
mean(c(1, 2, 3))  # or right after it
    
Try it Yourself โ†’

The Assignment Arrow <-

To store a value so you can use it later, use <-. Think of it as a little arrow pointing from the value to the name. You can read it as "gets" โ€” x <- 5 means "x gets 5". This is the R way of doing things, and you'll see it everywhere.


# Assigning values
x <- 10
y <- 20
x + y
    
Try it Yourself โ†’

Case Sensitivity

R is case-sensitive. Name and name are two completely different things. TRUE is not the same as true (and true will give you an error). Watch your capitalization โ€” it's one of the most common early mistakes.


# Case matters
myVar <- 100
myvar <- 200  # This is a different variable
print(myVar)  # Prints 100
    
Try it Yourself โ†’

That's the skeleton of R syntax. Now let's fill it in with variables โ€” the containers that hold all your data.

๐Ÿงช Quick Quiz

What symbol is used for assignment in R?