Introduction to R & RStudio
R Programming & Data Analytics / Introduction to R & RStudio

Introduction to R & RStudio

Beginner 10 hrs 4 Concepts
M1

Setting Up Your Environment

Concept 1

Installing R and RStudio

R is the language; RStudio is the IDE.

  1. Download R from cran.r-project.org — choose your OS.
  2. Download RStudio Desktop (free) from posit.co/downloads.
  3. Open RStudio — four panes: Console, Source, Environment, Files/Plots.
  4. Type version in the Console and press Enter to confirm R is working.

RStudio keyboard shortcuts:

  • Ctrl+Enter — run current line
  • Ctrl+Shift+Enter — run entire script
  • Alt+- — insert <- assignment arrow
  • Ctrl+Shift+M — insert |> pipe
Solved Examples
Example 1 Type R.version.string in the console. What does it return?

It returns a string like 'R version 4.3.1 (2023-06-16)'. This confirms R is installed and shows the version number.

Self-Assessment (3 questions)
Q1. What is the keyboard shortcut to insert the <- assignment operator in RStudio?
Q2. Which pane in RStudio shows loaded variables and data?
Q3. What command checks which R version is installed?
Concept 2

R Basics — Arithmetic and Assignment

R can be used as a calculator. The assignment operator <- (or =) stores values in named variables.

R
# Arithmetic
2 + 3 * 4          # 14 (operator precedence applies)
sqrt(144)          # 12
log(exp(1))        # 1

# Assignment
x <- 42
name <- "Vidaara"
is_ok <- TRUE

# Check type
class(x)           # "numeric"
class(name)        # "character"
class(is_ok)       # "logical"

# Multiple assignment
a <- b <- c <- 0   # all three equal 0

R is case-sensitive: Name and name are different variables.

Solved Examples
Example 1 Create variables: an integer 42L, a decimal 3.14, the text 'hello', and TRUE. Print the class of each.
R
x_int  <- 42L
x_dbl  <- 3.14
x_chr  <- 'hello'
x_lgl  <- TRUE
cat(class(x_int), class(x_dbl), class(x_chr), class(x_lgl))
# integer numeric character logical

The L suffix forces integer type instead of numeric (double).

Example 2 Find the remainder and quotient when 17 is divided by 5.
R
17 %% 5    # 2  (remainder)
17 %/% 5   # 3  (integer quotient)

R uses %% for modulo and %/% for integer division, unlike Python's % and //.

Self-Assessment (3 questions)
Q1. What does class(42L) return in R?
Q2. Which is the conventional assignment operator in R?
Q3. What does 5^3 compute in R?
M2

Vectors and Functions

Concept 3

Vectors — R's Core Data Structure

In R, everything is a vector. A single value like 42 is a length-1 vector. Vectors are homogeneous — all elements must be the same type.

R
# Create with c() — combine
scores <- c(85, 92, 78, 95, 88)
names_v <- c("Aarav", "Kavya", "Rohan")

# Vectorised operations — no loops needed!
scores + 5             # adds 5 to each
scores * 1.1           # 10% bonus to each
scores > 88            # logical vector: FALSE TRUE FALSE TRUE FALSE

# Subsetting (1-indexed!)
scores[1]              # 85
scores[c(1,3)]         # 85 78
scores[scores > 88]    # 92 95  — logical indexing
scores[-1]             # drop first element: 92 78 95 88

# Named vectors
setNames(scores, c("Math","English","Science","R","Stats"))

# Useful functions
length(scores)         # 5
sum(scores)            # 438
mean(scores)           # 87.6
range(scores)          # 78 95
which.max(scores)      # 4 (index of max)

Recycling rule: when operating on vectors of different lengths, R recycles the shorter one: c(1,2,3,4) + c(10,20) gives c(11,22,13,24).

R
# Try it yourself — run in RStudio console
scores <- c(85, 92, 78, 95, 88)
cat("Sum:", sum(scores), "\n")
cat("Mean:", mean(scores), "\n")
cat("Max:", max(scores), "\n")
scores > 88
Output
Sum: 438 
Mean: 87.6 
Max: 95 
[1] FALSE  TRUE FALSE  TRUE FALSE
Solved Examples
Example 1 From scores c(72,85,90,65,95,88,76,91), keep only scores above 80 and find their mean.
R
scores <- c(72,85,90,65,95,88,76,91)
high <- scores[scores > 80]
mean(high)  # mean(85,90,95,88,91) = 89.8
Example 2 Create a vector of even numbers from 2 to 20 using seq().
R
evens <- seq(2, 20, by = 2)
# or
evens <- seq(from=2, to=20, length.out=10)
# 2  4  6  8 10 12 14 16 18 20
Self-Assessment (3 questions)
Q1. What does c(1,2,3)[c(TRUE,FALSE,TRUE)] return?
Q2. R vectors are:
Q3. What is the result of length(1:10)?
Concept 4

Packages and the Tidyverse

R has 20,000+ packages on CRAN. The tidyverse is a collection of packages sharing a consistent design philosophy.

R
# Install (once)
install.packages("tidyverse")

# Load (every session)
library(tidyverse)

# Check what's loaded
search()

# Key tidyverse packages:
# dplyr    — data manipulation
# ggplot2  — visualisation
# tidyr    — reshaping
# readr    — fast CSV reading
# purrr    — functional programming
# stringr  — string manipulation
# forcats  — factor handling
# lubridate— dates

Install only once per machine; library() loads the package into the current session.

Solved Examples
Example 1 How do you see which version of dplyr is installed?
R
packageVersion("dplyr")   # e.g. '1.1.3'
# or
installed.packages()["dplyr", "Version"]
Self-Assessment (2 questions)
Q1. Which function loads a package into the current R session?
Q2. Which tidyverse package handles data manipulation with verbs like filter() and mutate()?
R Data Structures In Depth