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

Introduction to R & RStudio

Beginner 10 hrs 4 Concepts
Your Learning Map
📌 You already know
How to use a computer and follow steps — no programming experience needed.
🎯 You'll learn here
What R is, why data people choose it, and how to install R + RStudio and run your first print("Hello").
🌍 Where it's used
Every data role starts here — analysts at banks, ICMR researchers and government statisticians all open RStudio first.
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?
Alt+- (Alt plus minus) inserts <- in RStudio. This is the conventional R assignment arrow.
Q2. Which pane in RStudio shows loaded variables and data?
The Environment pane (top-right) shows all objects in the current R session including variables, data frames, and functions.
Q3. What command checks which R version is installed?
All three work. R.version.string gives a clean string; getRversion() gives a version object; R.version gives a list of version details.
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.

R — Variables & arithmetic LIVE READY
Output below is verified. Click to run real R in your browser (first run loads ~20 MB once).
Output (verified)
[1] 750
[1] 12
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?
The L suffix creates an integer. Without it, 42 is numeric (double-precision floating point).
Q2. Which is the conventional assignment operator in R?
While = works for assignment, <- is the tidyverse and CRAN convention. It visually separates assignment from function argument passing.
Q3. What does 5^3 compute in R?
R uses ^ for exponentiation. 5^3 = 125.
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
R — Your first vector LIVE READY
Output below is verified. Click to run real R in your browser (first run loads ~20 MB once).
Output (verified)
[1] 86.6
[1] 92
[1] 170 184 156 180 176
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?
Logical indexing: TRUE keeps the element, FALSE drops it. Positions 1 and 3 are TRUE, so we get elements 1 and 3.
Q2. R vectors are:
Vectors must be one type. Mixing types triggers coercion: logical→integer→double→complex→character.
Q3. What is the result of length(1:10)?
The : operator creates a sequence. 1:10 is c(1,2,3,4,5,6,7,8,9,10) which has length 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?
Both work but library() throws an error if the package is missing; require() returns FALSE. Use library() in scripts.
Q2. Which tidyverse package handles data manipulation with verbs like filter() and mutate()?
dplyr is the grammar of data manipulation. Its six core verbs cover 90% of data wrangling tasks.
R Data Structures In Depth