num_var <- 42.5
num_var[1] 42.5
num_var <- 42.5
num_var[1] 42.5
Create a character variable “char_var” with the value “R is fun!”.
char_var <- c("R is fun!")Print the data type “char_var”.
print(char_var)[1] "R is fun!"
Create a list “student_info” with the following elements:
student_info <- list (
name = c("Mohammad Nasir Abdullah"),
age = c(18),
grades = c(99, 100, 89))
student_info$name
[1] "Mohammad Nasir Abdullah"
$age
[1] 18
$grades
[1] 99 100 89
Create a data frame “df_students” with the following columns:
df_students <- data.frame(
Name = c("John", "Pablo"),
Age = c(22, 30),
Grades = c("A", "C")
)
df_students Name Age Grades
1 John 22 A
2 Pablo 30 C
Create a numeric vector “vec_num” with the values “5,10,15,20”.
vec_num <- c(5,10,15,20)
vec_num[1] 5 10 15 20
vec_num[c(2,3)][1] 10 15
vec_seq <- c(1:10)
vec_seq [1] 1 2 3 4 5 6 7 8 9 10
vec_rand <- c(1:100)
sample(vec_rand, size=5, replace=5)[1] 39 70 20 100 11
vec_char <- c("apple", "banana", "cherry")
vec_char[1] "apple" "banana" "cherry"
substr(vec_char, 1,3)[1] "app" "ban" "che"
vec_colors <- c("red", "blue", "green", "red", "blue")
vec_colors[1] "red" "blue" "green" "red" "blue"
factor_colors <- as.factor(vec_colors)print(factor_colors)[1] red blue green red blue
Levels: blue green red
Given a numeric vector “vec_bonus = c(10,20,30,40,50)”, write a code snippet to:
vec_bonus <- c(10,20,30,40,50)
vec_bonus[1] 10 20 30 40 50
a. Extract all elements greater than 25.
vec_bonus[vec_bonus>25][1] 30 40 50
b. Calculate the mean of the extracted elements.
mean(vec_bonus[vec_bonus>25])[1] 40
Given the following vectors:
weights <- c(55,68,72,61,58,75,64,70)
names <- (“Alice”, “Bob”, “Charlie”, ’Daisy”, “Eve”, “Frank”, “Grace”, “Henry”)
weights <- c(55,68,72,61,58,75,64,70)
weight_names <- c("Alice", "Bob", "Charlie", "Daisy", "Eve", "Frank", "Grace", "Henry")a. Identify the names of individuals whose weight is greater than 65kg.
weight_names[weights>65][1] "Bob" "Charlie" "Frank" "Henry"
b. Find the average weight of individuals weighting less than or equal to 60kg.
mean(weights[weights<=60])[1] 56.5
c. Extract the weights of individuals whose names start with the letter ‘A’ or ‘D’.
weights[substr(weight_names, 1, 1) %in% c("A","D")][1] 55 61
d. Determine the number of individuals whose weight is between 60kg and 70kg.
length(weights[weights >= 60 & weights<=70])[1] 4
Given the following vector of daily temperatures (in Celsius) for a week.
temperatures <- c(22,25,19,21,18,24,23)
days <- c(“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”)
temperatures <- c(22,25,19,21,18,24,23)
days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")Identify the days when the temperature was below 20 Celsius.
days[temperatures<20][1] "Wednesday" "Friday"
Calculate the difference between the highest and lowest temperatures of the week.
max(temperatures)-min(temperatures)[1] 7
Determine the number of days when the temperature was between 20 Celsius and 24 Celsius (inclusive).
length(days[temperatures >= 20 & temperatures <= 24])[1] 4
Given the following vector of exam scores:
scores <- c(85,78,92,65,88,70,95,80,60,90)
students <- c(“Anna”, “Ben”, “Cara”, “Dan”, “Ella”, “Finn”, “Grace”, “Hank”, “Ivy”, “)
scores <- c(85,78,92,65,88,70,95,80,60,90)
students <- c("Anna", "Ben", "Cara", "Dan", "Ella", "Finn", "Grace", "Hank", "Ivy", "John")Identify the student who scored above 90.
students[scores>90][1] "Cara" "Grace"
Find the average score of students who scored below 80.
mean(scores[scores<80])[1] 68.25
Extract the scores if students whose names start with a vowel (A,E,I.O,U).
scores[substr(students, 1, 1) %in% c("A","E", "I", "O", "U")][1] 85 88 60
Take Home Exercise
Basic Vector Creation: Create a numeric vector containing the numbers from 10 to 20 and a character vector containing the days of the week. Display both vectors.
num_vec <- c(10:20)
char_vec <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(num_vec) [1] 10 11 12 13 14 15 16 17 18 19 20
print(char_vec)[1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
[7] "Sunday"
Vector Concatenation: Given two vectors: a <- c(1,2,3) and b <- c(4,5,6), concatenate them to form a single vector.
a <- c(1,2,3)
b <- c(4,5,6)
a[1] 1 2 3
b[1] 4 5 6
d <- c(a, b)
d[1] 1 2 3 4 5 6
Element Extraction: Given the vector temperatures <- c(15,18,20,22,19,17,16), extract the temperatures for the 2nd, 4th, and 6th days.
temperature <- c(15,18,20,22,19,17,16)
temperature[1] 15 18 20 22 19 17 16
temperature[c(2,4,6)][1] 18 22 17
Omitting Elements: From the same temperatures vector, omit the temperature of the 5th day.
temperature[-5][1] 15 18 20 22 17 16
Vector Arithmetic: Given the vector prices <- c(10,20,30,40,50), calculate the new prices after a 10% discount.
prices <- c(10,20,30,40,50)
new_prices <- prices*0.9
new_prices[1] 9 18 27 36 45
Patterned Vectors: Create a vector that contains the numbers from 5 to 50, but only includes every 5th number (i.e. 5,10,15…)
vector_number <- seq(5,50, by = 5)
vector_number [1] 5 10 15 20 25 30 35 40 45 50
Random Patterns: Simulate the results of rolling a 6-sided dice 10 times.
sample(1:6, size = 10, replace = TRUE) [1] 3 3 3 3 1 3 3 2 3 6
Character Vector Operations: Given the vector fruits c(“apple”, “banana”, “cherry”), extract the first three letters of each fruits.
fruits <- c("apple", "banana", "cherry")
substr(fruits, 1, 3)[1] "app" "ban" "che"
Logical Vector Filtering: Given the vector ages <- c(25, 30 , 35, 40, 45, 50), identify which ages are greater than 30 and less than 50.
ages <- c(25, 30 , 35, 40, 45, 50)
ages[ages>30 & ages<50][1] 35 40 45
Relational Operators with Vectors: Given two vectors: vector1 <- c(5,10,15) and vector2 <-c(10,10,20), determine which elements of vector1 are less than or equal to the corresponding elements in vector2.
vectors1 <- c(5,10,15)
vectors2 <-c(10,10,20)
compares <- vectors1 <= vectors2
compares[1] TRUE TRUE TRUE
vectors1[vectors1 <= vectors2][1] 5 10 15