Introduction to R

R is a programming language built for statistics and data analysis — it lets you load data, compute summaries, run statistical models, and draw publication-quality charts with very little code.

Learn Introduction to R in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what R is and why analysts love it, how to use R as a calculator, how to store values in variables, and you'll have run your own first lines of R code.

What You'll Learn in This Lesson

1️⃣ Your First Lines of R

In R you don't need a main() function or any boilerplate. You type an expression and R evaluates it. To keep a value around for later, you assign it to a name with the operator (read it as "gets"). Read the worked example below — every line is explained — then run it yourself.

Notice the [1] in front of each result. That's not an error — it's R telling you the output starts at the first element. Because R treats even a single number as a one-element vector , every printed result carries an index. You'll see why that matters in the Vectors lesson.

2️⃣ R as a Calculator with Memory

R shines as an interactive calculator. You can do arithmetic directly, store intermediate results in variables, and call built-in functions using the name(arguments) pattern. The c() function ("combine") builds a vector — the structure R is built around.

In one line — mean(scores) — R averaged four numbers at once. That "do it to the whole collection" style is the heart of R and is what makes data analysis so concise.

3️⃣ Types and Getting Help

Every value in R has a type, which you can inspect with class() . When you're unsure how a function works, R's built-in help is excellent — type ?mean to open the documentation for mean .

Your turn. The script below works once you fill in the two # TODO blanks. Follow the hints, then run it and compare with the expected output.

No blanks this time — just a brief and an outline. Write it yourself, run it, and check your output against the example in the comments. Tiny programs like this build real fluency fast.

📋 Quick Reference — R Basics

Practice quiz

Which operator is the idiomatic way to assign a value in R?

  • =
  • ==
  • <-
  • :=

Answer: <-. While = works, the community convention for assignment is the <- arrow.

What does the [1] at the start of R's printed output mean?

  • An index marking the first element
  • An error code
  • The number of decimals
  • The line number

Answer: An index marking the first element. R treats values as vectors, so [1] marks the index of the first element shown.

Which function combines several values into a vector?

  • vector()
  • list()
  • bind()
  • c()

Answer: c(). c() ('combine') builds R's fundamental data structure, the vector.

What does mean(c(80, 92, 75, 88)) return?

  • 88
  • 83.75
  • 335
  • 92

Answer: 83.75. mean() averages the four numbers: (80+92+75+88)/4 = 83.75.

Which function tells you the type of a value?

  • typeof2()
  • kind()
  • class()
  • is()

Answer: class(). class() reports the type, e.g. "character", "numeric", or "logical".

How do you open the help page for the mean function?

  • ?mean
  • help!mean
  • doc(mean)
  • man mean

Answer: ?mean. Typing ?mean opens R's built-in documentation for that function.

Why is R case-sensitive a common source of errors?

  • It ignores spaces
  • It rounds numbers
  • It auto-corrects names
  • Mean() is not the same as mean()

Answer: Mean() is not the same as mean(). R distinguishes case, so Mean() is undefined while mean() works.

What does sqrt(144) return?

  • 72
  • 144
  • 12
  • 24

Answer: 12. sqrt() gives the square root, and the square root of 144 is 12.

What does class(TRUE) return?

  • "logical"
  • "boolean"
  • "numeric"
  • "character"

Answer: "logical". TRUE and FALSE are logical values in R, so class(TRUE) is "logical".

What is R primarily designed for?

  • Game development
  • Operating systems
  • Statistics and data analysis
  • Mobile apps

Answer: Statistics and data analysis. R was created by statisticians for statistics, data analysis, and visualization.