WEEK 1 LAB EXERCISE – Getting Started with R and RStudio
Duration: 2 hours
Mode: Guided and Hands-on Practice
Module: CT127-3-2 Programming for Data Analysis
Lecturer: Dr. Kulothunkan Palasundram (Dr. Kulo)
Objective
By the end of this lab, students will be able to:
1. Install R and RStudio
2. Type commands in console
3. Navigate RStudio
4. Create new scripts
5. Set up working directories.
6. Create and manipulate variables and vectors.
Lab Structure
1. Setup & Environment
2. Console commands
3. Variables & Vectors
Setup & Environment
Tasks:
1. Install R engine : [Link]
Click on the link below to and follow the instructions to download and install R.
Complete the installation first before proceeding to step 2. You must finish the
installation of R engine before you can install RStudio.
1
2. Install RStudio : [Link]
Click on the link below to and follow the instructions to download and install
RStudio.
1. After RStudio has been installed successfully, launch it.
Practise 1: Console commands
Run these lines one by one and watch the Console and Environment panes. Type the
commands below and press enter to see the output, then proceed with the next
command.
Part A – Simple Commands
2+3 # Adds two numbers → 5
x <- 10 # Create object x with value 10
y <- 4 # Create object y with value 4
x+y # 14 (addition)
x-y # 6 (subtraction)
x*y # 40 (multiplication)
x/y # 2.5 (division)
2
rm(y) # Remove y from the Environment
ls() # List objects (should include 'x')
Notes:
• Assignment uses the left arrow <- .
• Environment pane shows created objects; Console shows outputs.
• Use Ctrl/Cmd+Enter to run the current line from the Script.
Part B — Vectors & Basic Functions
Run these lines one by one and watch the Console and Environment panes. Type the
commands below and press enter to see the output, then proceed with the next
command.
Create three vectors and practice summaries, indexing, and sequences.
# logical vector
flags <- c(TRUE, FALSE, TRUE)
# character vector (categories)
chars <- c("red", "blue", "blue", "green")
unique(chars) # 'red' 'blue' 'green'
table(chars) # blue 2, green 1, red 1
# numeric vector with a missing value
nums <- c(2, 5, 7, 7, 10, NA)
length(nums)
class(nums)
[Link](nums)
sum(nums, [Link] = TRUE)
mean(nums, [Link] = TRUE)
median(nums, [Link] = TRUE)
var(nums, [Link] = TRUE)
nums[1]
nums[2:4]
nums[c(1, 6)]
nums[]
3
seq(1, 10, by = 2) #13579
rep(0, times = 5) #00000
?mean # Open Help for mean()
Notes:
• [Link]=TRUE tells functions to ignore missing values (NAs).
• Indexing uses 1-based positions; logical indexing keeps elements where the condition is
TRUE.
Practise 2: Working with Scripts
Repeat Part A and Part B using scripts
Create a new script file and save it into an existing folder. Provide any suitable name
for it.
Type in the commands from practice 1 and run them line by line and watch the
console window for output.
Practise 3: Set Working Directory
Create another new script. Write the commands below and then run them line by
line and watch the console window for the output.
getwd() # shows your current/default working directory
#set new working directory
setwd("put in a valid location in your computer")
getwd() #confirm working directory has been set correctly
Practise 4: Variables and Vectors
Note: As in any other programming or scripting languages, we can create and make
use of variables of several data types to store values and be used in our operations.
Variables enable reuse of values for multiple processing. Create a new script for
practice 4.
4
Part A – Creating Variables
name <- "Ali"
age <- 22
height <- 1.75
weight <- 68
bmi <- weight / (height^2)
bmi
Expected Output: 22.2
Explanation: BMI is a derived variable calculated from height and weight.
Part B – Working with Vectors
scores <- c(55, 68, 74, 80, 90)
mean(scores) # average
sd(scores) # spread
max(scores) # highest score
min(scores) # lowest score
Expected Output: Mean = 73.4, SD ≈ 13.54
Explanation: Vectors store ordered numeric or text values.
Part C – Vector Arithmetic
bonus <- c(5, 5, 5, 5, 5)
final_scores <- scores + bonus
final_scores
Expected Output: 60 73 79 85 95
Explanation: Vector arithmetic applies operations element-wise.
Part D – Logical and Character Vectors
students <- c("Ali", "Bala", "Chong", "Deepa", "Esha")
passed <- final_scores >= 70
[Link](students, final_scores, passed)
Expected Output: Table showing TRUE/FALSE for each student’s pass status.
<end of exercise>