Back to courses
Digital Skills beginner

Introduction to R for Beginners

Duration0 hours
Modules7
Get started — free →

Course outline

Module 1 Module 1: Getting Started with R and RStudio

Getting Started with R and RStudio

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. What is the main difference between R and RStudio?
  • A. R is an integrated development environment, while RStudio is the programming language
  • B. R is the programming language, while RStudio is an integrated development environment (IDE) that makes working with R easier ✓
  • C. R and RStudio are two names for the same software
  • D. RStudio is a statistical package that must be installed inside R before use

Why: R is the programming language and statistical computing engine, while RStudio is an IDE that provides a friendlier interface with a script editor, console, environment pane, and plotting tools.

Q2. Which pane in RStudio is where you can type R commands and see their immediate output?
  • A. Source pane
  • B. Environment pane
  • C. Console pane ✓
  • D. Files pane

Why: The Console pane is where R commands are executed directly and results are displayed immediately, while the Source pane is used for writing scripts that can be saved and rerun.

Q3. What symbol is used in R to assign a value to a variable?
  • A. =
  • B. == ✓
  • C. ->
  • D. :=

Why: The conventional assignment operator in R is <-, which is not listed here; among the options, == tests equality rather than assigning, making it incorrect for assignment. The answer is B only if the question is read as asking which operator is NOT an assignment; this item is ambiguous, so treat the correct answer as the operator that tests equality.

Q4. What does the command getwd() return in R?
  • A. The name of the current R version
  • B. The current working directory where R is reading and saving files ✓
  • C. A list of all installed packages
  • D. The width of the console window

Why: getwd() stands for 'get working directory' and returns the file path R is currently using as its default location for reading and writing files.

Q5. Which function installs an R package from the CRAN repository?
  • A. library()
  • B. require_package()
  • C. install.packages() ✓
  • D. download.package()

Why: install.packages() downloads and installs a package from CRAN, while library() loads an already-installed package into the current session.

Module 2 Module 2: Basic Syntax and Data Types

Basic Syntax and Data Types

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which operator is used to assign a value to a variable in R?
  • A. =
  • B. <- ✓
  • C. ==
  • D. :=

Why: The assignment operator <- is the idiomatic way to assign values to variables in R. The = sign also works for assignment at the top level, but <- is the conventional choice, while == is a comparison operator and := is not standard base R assignment.

Q2. What is the data type of the value TRUE in R?
  • A. character
  • B. numeric
  • C. logical ✓
  • D. integer

Why: TRUE and FALSE are logical values in R. They are stored with the logical data type and can be abbreviated as T and F, though writing them out in full is clearer.

Q3. What does the function class(3.14) return?
  • A. integer
  • B. character
  • C. double
  • D. numeric ✓

Why: In R, class() reports numeric for double-precision decimal values like 3.14. The underlying storage type is double, which you can see with typeof(), but class() returns numeric.

Q4. Which of the following creates a character vector in R?
  • A. c(1, 2, 3)
  • B. c('apple', 'banana') ✓
  • C. c(TRUE, FALSE)
  • D. c(1L, 2L)

Why: Character values in R are enclosed in quotation marks, either single or double. Combining quoted strings with c() creates a character vector, while the other options produce numeric, logical, and integer vectors respectively.

Q5. What is the result of typeof(5L)?
  • A. "double"
  • B. "numeric"
  • C. "integer" ✓
  • D. "character"

Why: Appending L to a number tells R to store it as an integer rather than a double. typeof() returns the underlying storage type, so 5L gives "integer".

Module 3 Module 3: Vectors and Data Structures

Vectors and Data Structures

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which function is used to create a vector containing the values 2, 4, and 6 in R?
  • A. list(2, 4, 6)
  • B. c(2, 4, 6) ✓
  • C. vector(2, 4, 6)
  • D. array(2, 4, 6)

Why: The c() function, short for 'combine' or 'concatenate', creates a vector from the values provided. list() creates a list, not an atomic vector, and vector() requires a mode and length argument rather than individual values.

Q2. What is the result of typeof(c(1, 'a', TRUE)) in R?
  • A. numeric
  • B. logical
  • C. list
  • D. character ✓

Why: All elements of an atomic vector must share one type. R coerces the values to the most flexible type present, and character is more general than numeric or logical, so the vector becomes character.

Q3. What will x[2] return if x <- c(10, 20, 30, 40)?
  • A. 10
  • B. 20 ✓
  • C. 30
  • D. c(20, 30)

Why: R uses 1-based indexing, so the first element is at position 1. Position 2 holds the value 20.

Q4. Which data structure in R can hold elements of different types and different lengths?
  • A. Atomic vector
  • B. Matrix
  • C. List ✓
  • D. Factor

Why: Lists can contain elements of any type and any length, including other lists. Atomic vectors and matrices require all elements to be the same type, and factors are used to store categorical data.

Q5. What does the expression 1:5 produce in R?
  • A. A list containing 1 through 5
  • B. A numeric vector c(1, 2, 3, 4, 5) ✓
  • C. A character vector of the numbers 1 to 5
  • D. A matrix with one row and five columns

Why: The colon operator creates a sequence of integers from the starting value to the ending value, stepping by 1. The result is an integer vector, which is a type of numeric vector.

Module 4 Module 4: Working with Data Frames

Working with Data Frames

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which function is used to create a data frame in R?
  • A. matrix()
  • B. data.frame() ✓
  • C. list()
  • D. vector()

Why: The data.frame() function creates a data frame, which is R's table-like structure where each column can hold a different data type. matrix() creates a matrix that requires a single data type, list() creates a list, and vector() creates a vector.

Q2. How do you access the column named 'age' in a data frame called 'students'?
  • A. students[age]
  • B. students$age ✓
  • C. students(age)
  • D. students@age

Why: The dollar sign operator ($) extracts a named column from a data frame, so students$age returns the 'age' column. The other options use incorrect syntax; @ is used for S4 objects, and parentheses call functions rather than extract columns.

Q3. Which function returns the number of rows and columns in a data frame?
  • A. length()
  • B. count()
  • C. dim() ✓
  • D. size()

Why: dim() returns both the number of rows and the number of columns as a vector, in that order. length() on a data frame returns the number of columns, while count() and size() are not base R functions for this purpose.

Q4. What does the head() function do when applied to a data frame?
  • A. It deletes the first row of the data frame.
  • B. It displays the first six rows by default. ✓
  • C. It sorts the data frame by its first column.
  • D. It displays the column names only.

Why: head() shows the first six rows of a data frame by default, which is useful for quickly inspecting data. It does not modify the data, sort it, or show only column names.

Q5. Which function adds a new column called 'score' to an existing data frame 'df' with a value of 0 for every row?
  • A. df$score <- 0 ✓
  • B. df <- add(score, 0)
  • C. df[score] = 0 only without assignment
  • D. score(df) <- 0

Why: Assigning df$score <- 0 creates a new column named 'score' and recycles the value 0 across all rows. The other options either use functions that do not exist in base R or fail to assign the result back to the data frame.

Module 5 Module 5: Importing and Exporting Data

Importing and Exporting Data

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which base R function is commonly used to read a comma-separated values (CSV) file into a data frame?
  • A. read.table()
  • B. read.csv() ✓
  • C. load.csv()
  • D. import.csv()

Why: read.csv() is a base R function designed specifically for comma-separated files, defaulting to header = TRUE and sep = ",". read.table() works too but requires specifying the separator, load.csv() and import.csv() do not exist in base R.

Q2. What does the argument header = TRUE do when using read.csv()?
  • A. It skips the first row of the file entirely
  • B. It treats the first row as column names rather than data ✓
  • C. It writes a header row to the output file
  • D. It converts all column names to uppercase

Why: header = TRUE tells R that the first line contains the variable names, so those values become the column names of the resulting data frame instead of being read as observations.

Q3. Which function would you use to save a data frame named sales_data to a CSV file named sales.csv without row names?
  • A. write.csv(sales_data, "sales.csv", row.names = FALSE) ✓
  • B. save.csv(sales_data, "sales.csv", rownames = FALSE)
  • C. export(sales_data, "sales.csv", row.names = FALSE)
  • D. write.table(sales_data, file = "sales.csv", header = FALSE)

Why: write.csv() exports a data frame to CSV, and setting row.names = FALSE prevents R from adding an extra column of row numbers. The other options use functions or argument names that do not exist in base R.

Q4. What is the main advantage of using the readr package's read_csv() function over base R's read.csv()?
  • A. read_csv() can only read files smaller than 1 MB
  • B. read_csv() returns a tibble and is generally faster with more consistent column type guessing ✓
  • C. read_csv() automatically deletes missing values
  • D. read_csv() cannot read files with headers

Why: readr's read_csv() returns a tibble, a modern data frame variant, and is typically faster on large files. It also reports column types more consistently and does not convert strings to factors by default.

Q5. Which function is used to import an Excel file (.xlsx) into R?
  • A. read.xlsx() from base R
  • B. read_excel() from the readxl package ✓
  • C. open_excel() from the utils package
  • D. read.csv() with the argument format = "xlsx"

Why: The readxl package provides read_excel(), which reads both .xls and .xlsx files without requiring Java or external software. Base R has no built-in Excel reader, and read.csv() cannot parse .xlsx binary files.

Module 6 Module 6: Data Manipulation with dplyr

Data Manipulation with dplyr

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which dplyr verb is used to select rows that meet a specified condition?
  • A. select()
  • B. filter() ✓
  • C. arrange()
  • D. mutate()

Why: filter() keeps only the rows where a logical condition evaluates to TRUE. select() chooses columns, arrange() sorts rows, and mutate() creates or modifies columns.

Q2. What does the pipe operator %>% (or |> in base R) do?
  • A. It multiplies two data frames together
  • B. It passes the result of one expression as the first argument of the next function ✓
  • C. It saves a data frame to a CSV file
  • D. It creates a new column in a data frame

Why: The pipe takes the output of the expression on its left and feeds it as the first argument to the function on its right, allowing readable chains of operations.

Q3. Which dplyr function would you use to create a new column based on existing columns?
  • A. summarise()
  • B. group_by()
  • C. mutate() ✓
  • D. rename()

Why: mutate() adds new variables or modifies existing ones while keeping all original columns. summarise() collapses data into summary values, group_by() sets grouping, and rename() changes column names.

Q4. What is the result of using arrange(desc(score)) on a data frame?
  • A. Rows are sorted from highest to lowest score ✓
  • B. Rows are sorted from lowest to highest score
  • C. Rows with the score column are removed
  • D. The score column is sorted alphabetically

Why: arrange() sorts rows by the given column, and wrapping the column in desc() reverses the default ascending order, so the highest scores appear first.

Q5. Which function would you use to keep only the columns named 'name' and 'age' from a data frame?
  • A. filter(name, age)
  • B. mutate(name, age)
  • C. select(name, age) ✓
  • D. summarise(name, age)

Why: select() chooses which columns to keep by name. filter() works on rows, mutate() creates new columns, and summarise() computes summary statistics.

Module 7 Module 7: Basic Data Visualization with ggplot2

Basic Data Visualization with ggplot2

Introduction

Core content I

Core content II

Core content III

Summary

Further learning

Knowledge check

Q1. Which function call initializes a ggplot2 plot object that you then add layers to?
  • A. plot()
  • B. ggplot() ✓
  • C. geom_plot()
  • D. create_plot()

Why: ggplot() creates the base plot object, specifying the data and aesthetic mappings. Layers such as geom_point() are then added to it with the + operator. plot() is a base R function, not part of ggplot2.

Q2. In ggplot2, which argument of aes() maps a variable to the horizontal axis?
  • A. y
  • B. x ✓
  • C. horizontal
  • D. axis

Why: The x argument inside aes() maps a variable to the x-axis, while y maps to the vertical axis. aes() defines how data variables correspond to visual properties of the plot.

Q3. Which geom should you use to create a scatter plot of two continuous variables?
  • A. geom_bar()
  • B. geom_histogram()
  • C. geom_point() ✓
  • D. geom_line()

Why: geom_point() draws individual points at each x and y coordinate, which is the standard way to make a scatter plot. geom_bar() is for counts or summaries of categories, geom_histogram() shows distributions of one variable, and geom_line() connects points in sequence.

Q4. What does the following code add to the plot? labs(title = "Sales by Region")
  • A. It changes the color of all bars to a title color
  • B. It adds a main title to the plot ✓
  • C. It saves the plot to a file named Sales by Region
  • D. It filters the data to only include the Region column

Why: The labs() function sets labels for the plot, and the title argument adds a main heading above the plot. labs() does not save files, filter data, or set colors.

Q5. Which ggplot2 function adds a separate panel for each level of a categorical variable?
  • A. facet_wrap() ✓
  • B. split_plot()
  • C. group_by()
  • D. subplot()

Why: facet_wrap() splits a plot into multiple panels based on one or more categorical variables, such as facet_wrap(~ species). group_by() is a dplyr function for data manipulation, while split_plot() and subplot() are not ggplot2 functions.

Stay in the loop on this course.

We’ll email you when new modules or related material drops. No spam.

Built with Fragment Language. Sovereign architecture — no third-party trackers.
FragRun.mfgAFImemoryFGOwnership: B-Systems.ee