Introduction to R Programming
A Beginner's Learning Module | Concepts, Examples & Practice Q&A
What's inside this module:
● What R is and why it's used for data analysis and statistics
● Core building blocks: variables, data types, and vectors
● Working with data frames and importing data
● Writing your own functions and using control flow
● Basic data visualization with base R plotting
● A 10-question practice quiz with full answers to test your understanding
1. What is R?
R is a free, open-source programming language and software environment built for statistical computing,
data analysis, and graphics. It is widely used by statisticians, data scientists, and researchers because of
its powerful built-in statistical functions and its enormous ecosystem of add-on packages available
through CRAN (the Comprehensive R Archive Network).
Most people work with R through RStudio, a popular integrated development environment (IDE) that
adds a console, script editor, plot viewer, and package manager on top of base R.
Why learn R?
• Purpose-built for statistics, data wrangling, and visualization
• Huge library ecosystem (e.g., ggplot2, dplyr, tidyverse)
• Free and open source, with strong community support
• Widely used in academia, research, finance, and data science roles
2. Variables and Data Types
In R, you create a variable using the assignment operator <- (though = also works). R is dynamically
typed, meaning you don't need to declare a variable's type in advance.
x <- 10 # numeric
name <- "Alice" # character (string)
is_active <- TRUE # logical
y <- 3L # integer
z <- 2 + 3i # complex
The main basic data types in R are:
Type Example Description
numeric 10, 3.14 Decimal or whole numbers
integer 3L Whole numbers (L suffix)
character "hello" Text strings
logical TRUE / FALSE Boolean values
complex 2+3i Complex numbers
Use class() to check a variable's type and str() to inspect its structure.
class(x) # "numeric"
class(name) # "character"
3. Vectors: The Building Block of R
A vector is an ordered collection of values of the same type, and it's the most fundamental data structure
in R. You create one with the c() (combine) function.
ages <- c(23, 35, 41, 19)
names <- c("Amir", "Bela", "Chen")
ages[2] # 35 (R indexing starts at 1)
ages * 2 # 46 70 82 38 (vectorized operation)
mean(ages) # 29.5
length(ages) # 4
R indexing starts at 1, not 0, and most operations on vectors are vectorized — meaning a function or
operator is automatically applied to every element without needing a loop.
4. Data Frames
A data frame is a table-like structure where each column can hold a different data type — similar to a
spreadsheet or a SQL table. It's the most common structure for storing real datasets in R.
students <- [Link](
name = c("Amir", "Bela", "Chen"),
score = c(88, 92, 79),
passed = c(TRUE, TRUE, FALSE)
)
students$score # access the score column
nrow(students) # number of rows
summary(students) # quick statistical summary
Data is often imported into a data frame from a CSV file using [Link]():
df <- [Link]("[Link]")
head(df) # preview the first 6 rows
5. Functions and Control Flow
You can write your own reusable functions with the function() keyword. Control flow statements like
if/else and loops (for, while) work much like in other programming languages.
square <- function(x) {
return(x^2)
}
square(5) # 25
for (i in 1:5) {
if (i %% 2 == 0) {
print(paste(i, "is even"))
}
}
6. Basic Data Visualization
Base R includes simple plotting functions, and the ggplot2 package (part of the tidyverse) is the most
popular tool for more polished, layered graphics.
# Base R
plot(students$score, type = "b", main = "Student Scores")
hist(students$score)
# ggplot2
library(ggplot2)
ggplot(students, aes(x = name, y = score)) +
geom_col()
7. Practice Quiz: 10 Questions & Answers
Test your understanding of the concepts covered in this module. Try to answer each question yourself
before checking the answer below it.
Q1. What symbol is most commonly used in R for variable assignment?
Answer: The arrow operator <- is the conventional assignment operator in R (e.g., x <- 5),
although = can also be used in most contexts.
Q2. What function is used to create a vector in R?
Answer: The c() function (short for "combine") is used to create a vector, e.g. v <- c(1, 2,
3).
Q3. Does R indexing start at 0 or 1?
Answer: R indexing starts at 1. So for a vector v <- c(10, 20, 30), v[1] returns 10, not 20.
Q4. What data structure would you use to store a table with columns of different
types (e.g., names as text and scores as numbers)?
Answer: A data frame, created with [Link](), is designed exactly for this — each column
can hold a different data type.
Q5. What does the function class(x) do?
Answer: It returns the data type (class) of the object x, such as "numeric", "character", or "logical".
Q6. What is meant by a 'vectorized' operation in R?
Answer: It means an operation or function is automatically applied to every element of a vector at
once, without needing an explicit loop. For example, c(1,2,3) * 2 returns c(2,4,6) directly.
Q7. Which function reads a CSV file into a data frame?
Answer: [Link]("[Link]") reads a CSV file and loads it into a data frame.
Q8. How do you define your own function in R?
Answer: Using the function() keyword, for example: square <- function(x) {
return(x^2) }.
Q9. What package is most commonly used for advanced, polished data visualization
in R?
Answer: ggplot2, part of the tidyverse collection of packages, is the most widely used tool for
building layered, publication-quality graphics in R.
Q10. What logical data type values does R use to represent true and false?
Answer: R uses TRUE and FALSE (often abbreviated T and F) as its logical (boolean) values.