Introduction to R
Reviewed & published by Brayan K
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.
Part of the free R course at LearnCodingFast — hands-on lessons with worked examples and the output they print, 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
- What R is and why it's the go-to language for statistics
- How to print values and run calculations in the R console
- How to store values in variables with the <- operator
- How R is vectorized and what the [1] in output means
- How to call built-in functions like sqrt(), round(), and mean()
- How to check types with class() and get help with ?
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.
# This is an R script. Lines that begin with # are comments — R ignores them.
# In R, you usually print results just by typing an expression at the console.
# print() shows a value explicitly. The [1] in the output is an index marker.
print("Hello from R!") # -> [1] "Hello from R!"
# R is built for numbers. Type a calculation and it answers immediately.
2 + 2 # -> [1] 4
# The assignment operator in R is <- (you can also use =, but <- is idiomatic).
total <- 10 * 3
print(total) # -> [1] 30Notice 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.
# Everything in R is vectorized — even a single number is a length-1 vector,
# which is why R prints [1] in front of results.
revenue <- 1200 # store a value with <-
costs <- 750
profit <- revenue - costs
profit # -> [1] 450
# Built-in functions follow the name(args) pattern.
sqrt(144) # -> [1] 12
round(3.14159, 2) # -> [1] 3.14
# c() combines values into a vector — R's most fundamental data structure.
scores <- c(80, 92, 75, 88)
mean(scores) # -> [1] 83.75In 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.
# R ships with rich built-in help. Run these in the console.
# ?function opens the documentation page for a function:
# ?mean
# Find which package a function lives in, and confirm R is working:
# R.version.string # prints YOUR installed version, e.g. "R version 4.3.3 (2024-02-29)"
# class() tells you what type of thing a value is:
class("text") # -> [1] "character"
class(42) # -> [1] "numeric"
class(TRUE) # -> [1] "logical"Your turn. The script below works once you fill in the two ___ blanks. Follow the hints, then run it and compare with the expected output.
# 🎯 YOUR TURN — replace each ___ then run it in R or RStudio.
# 1) Store your name in a variable using the <- operator
name <- ___ # 👉 replace ___ with "Sam" (or your own name, in quotes)
# 2) Store your favourite number
fav <- ___ # 👉 replace ___ with 7 (or any number you like)
# 3) Print a value and a calculation
print(name)
print(fav * 2)
# ✅ Expected output (for "Sam" and 7):
# [1] "Sam"
# [1] 14Mini-Challenge: An "About Me" Report
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.
Common Errors (and the fix)
- "could not find function 'Mean'" — R is case-sensitive. The function is mean(), not Mean(). Check capitalisation of every function and variable name.
- "object 'x' not found" — You used a variable before assigning it, or mistyped its name. Assign it first with x <- 5, then use it.
- Using < - with a space — x < - 5 means "is x less than -5?", not assignment. Write the arrow with no space: x <- 5.
- Unbalanced quotes or parentheses — If the console shows a + prompt, R is waiting for you to finish an expression. Press Esc and check for a missing " or ).
Pro Tips
- 💡 Use <- for assignment: while = works, the community convention is <-. In RStudio, press Alt+- to type it instantly.
- 💡 Try snippets instantly: use a free online runner like rdrr.io/snippets to test code without installing anything.
- 💡 Read the [1]: it's an index, not an error. Long results wrap onto several lines, each starting with the index of its first value.
📋 Quick Reference — R Basics
| Task | R Syntax |
|---|---|
| Assign a value | x <- 5 |
| Print a value | print(x) |
| Combine into a vector | c(1, 2, 3) |
| Average a vector | mean(x) |
| Check a type | class(x) |
| Open help | ?mean |
🎉 Lesson Complete!
- ✅ R is a language built for statistics and data analysis
- ✅ You assign values with the <- operator
- ✅ R is vectorized — the [1] in output is an index
- ✅ Call functions with name(arguments), e.g. mean(scores)
- ✅ Check types with class() and get help with ?
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.
Continue this course
- Next: RStudio & Running Code — Set up RStudio, use the console and scripts, and run code interactively
- Quick reference: R cheat sheet › Basics & Vectors
Frequently asked questions
What exactly is R, and what is it used for?
R is a programming language and free software environment built specifically for statistics, data analysis, and visualization. It was created by statisticians, so things that are awkward in general-purpose languages — fitting models, summarizing data, drawing charts — are first-class and concise in R. It's widely used in academia, research, bioinformatics, finance, and data science. If your goal is to explore data, run statistical tests, or produce graphs and reports, R is one of the best tools available.
Is R hard to learn if I've never programmed before?
R is very approachable for beginners because you can get useful results from your very first line — typing 2 + 2 or mean(c(1, 2, 3)) returns an answer immediately, with no setup or boilerplate. You don't need to know about classes, compilers, or build tools to be productive. The main idea to absorb early is that R works on vectors (whole columns of data at once) rather than one value at a time, which this course introduces gently.
What is the difference between R and Python for data work?
Both are excellent for data science and share many concepts. R was designed from the ground up for statistics and has unmatched depth in statistical modeling, along with ggplot2 for visualization and the tidyverse for data wrangling. Python is a general-purpose language that is also strong in data work and machine learning. Many analysts learn both. This course focuses purely on R; the skills (vectors, data frames, grouping, modeling) transfer conceptually to any data tool.
Do I need to install anything to start?
To run R on your own machine you install R from cran.r-project.org and, optionally, RStudio (a friendly editor) from posit.co. But you don't need to install anything to learn from this page — you can paste any example into a free online R runner and see the same output shown here. The next lesson walks through setting up RStudio so you have a comfortable place to write and run code.