Chapter 4: R Syntax

R: its syntax.

Think of syntax as the grammar rules of the R language. If you break them, R gets confused and throws errors (those red messages in the console). But once you get comfortable, R syntax feels very natural — especially if you’re coming from stats/math background rather than heavy programming.

I’m going to teach this like we’re sitting together at a table in Hi-Tech City, writing code line by line on my laptop, explaining why each rule exists, with tons of examples you can copy-paste right now into RStudio.

1. Core Philosophy of R Syntax (Important Mindset First)

  • R is case-sensitive → mean() ≠ Mean() ≠ MEAN()
  • Almost everything in R is a function — even operators like + are secretly functions
  • You mostly work with objects (variables) — you create them, name them, manipulate them
  • No semicolons needed at end of lines (unlike Java/C++) — new line = new command
  • Use spaces freely for readability — R ignores extra spaces (mostly)
  • Comments start with # — everything after # on that line is ignored

2. The Most Important Symbol: Assignment Operator (<-)

This is the single most characteristic thing in R syntax.

R

Why <- instead of = ? Because = is also used for naming arguments inside functions. Using <- everywhere avoids confusion.

Bad (confusing):

R

Good (clear):

R

3. Comments – Your Best Friend

R

Pro tip: Good code has ~30–50% comments when you’re learning.

4. Printing / Showing Results

In RStudio console:

  • Just type the name → it prints (implicit print)
R
  • Explicit print (useful in scripts/functions)
R
  • Cat for nicer output (no [1] prefix)
R

5. Basic Building Block: Vectors (Everything Starts Here)

Vectors are the atoms of R — almost all data lives in vectors.

Syntax to create:

R

Vector operations happen element-wise — huge time-saver!

R

6. Functions – How You Do Almost Everything

Syntax pattern:

R

Examples:

R

7. Data Frames – The Excel-like Table (Most Important Structure)

Syntax to create manually:

R

Access syntax – very flexible:

R

Modern tidyverse style (learn soon):

R

8. Control Structures – If, For, While (Less Used in Modern R)

R

9. Quick Reference Table – Most Common Syntax Patterns

What you want to do Syntax Example Notes
Assign value x <- 10 or x = 10 Prefer <-
Create vector c(1, 2, 3) or 1:10 : is fast for integers
Comment # explanation Everywhere!
Function call mean(x, na.rm = TRUE) na.rm = named argument
Access column df$column or df[[“column”]] $ is friendly
Logical AND / OR & and (single for vectors)
String combine paste(“Hi”, “Hyd”, sep = ” “) or paste0() no space
Install package install.packages(“tidyverse”) Internet needed
Load package library(tidyverse) Every new session

10. Your Mini Practice Right Now (Copy → Run)

R

You just used vectors, data frames, logical subsetting, functions, ifelse(), assignment — core R syntax!

Feeling good?

Tell me:

  • Want more on subsetting tricks?
  • Control flow (loops/if) in detail?
  • Or jump to dplyr / ggplot2 syntax next?
  • Any line giving error right now?

I’m right here — let’s fix or go deeper! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *