num_var <- 42.5
num_var[1] 42.5
num_var <- 42.5
num_var[1] 42.5
char_var <- "R is fun!"print(char_var)[1] "R is fun!"
student_info <- list(
name = c("Mohammad Nasir Abdullah"),
age = 18,
grades = c("99, 100, 89")
)
student_info$name
[1] "Mohammad Nasir Abdullah"
$age
[1] 18
$grades
[1] "99, 100, 89"
f_students <- data.frame(
Name = c("John", "Pablo"),
Age = c("22", "30"),
Grade = c("A", "C")
)
f_students Name Age Grade
1 John 22 A
2 Pablo 30 C
vec_num <- c(5, 10, 15, 20)
vec_num[1] 5 10 15 20
vec_num[1] 5 10 15 20
vec_num[2:3][1] 10 15
vec_seq <- seq(1,10)
vec_seq [1] 1 2 3 4 5 6 7 8 9 10
vec_rand <- sample(1:100,5)
vec_rand[1] 25 51 77 95 68
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)
class(factor_colors)[1] "factor"
levels(factor_colors)[1] "blue" "green" "red"
# 1)
vec_bonus <- c(10, 20, 30, 40, 50)
vec_bonus[vec_bonus > 25][1] 30 40 50
# 2)
vec_bonus[1] 10 20 30 40 50
mean(vec_bonus[vec_bonus > 25])[1] 40
Given the following vectors:
weights ‹- c(55, 68, 72, 61, 58, 75, 64, 70)
names ‹- c(“Alice”, “Bob”, “Charlie”, “Daisy”, “Eve”, “Frank”, “Grace”, “Henry”)
weights <- c(55, 68, 72, 61, 58, 75, 64, 70)
names <- c("Alice", "Bob", "Charlie", "Daisy", "Eve", "Frank", "Grace", "Henry")# a)
names[weights > 65][1] "Bob" "Charlie" "Frank" "Henry"
# b)
mean(weights[weights <= 60])[1] 56.5
# c)
weights[substr(names,1,1) %in% c("A","D")][1] 55 61
sum(weights >= 60 & weights <= 70)[1] 4
Hint: You might find functions like mean(), substr(), and length() useful.
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")a. Identify the days when the temperature was below 20°C.
b. Calculate the difference between the highest and lowest temperatures of the week.
c. Determine the number of days when the temperature was between 20°C and 24°C (inclusive).
# a)
days[temperatures < 20][1] "Wednesday" "Friday"
# b)
max(temperatures) - min(temperatures)[1] 7
# c)
sum(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", "Jay")# a)
students[scores > 90][1] "Cara" "Grace"
mean(scores[scores < 80])[1] 68.25
scores[substr(students, 1, 1) %in% c("A", "E", "I", "O", "U")][1] 85 88 60
Take Home Exercise
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
days_vector[1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"
[7] "Sunday"
num_vector <- 10:20
num_vector [1] 10 11 12 13 14 15 16 17 18 19 20
a <- c(1, 2, 3)
b <- c(4, 5, 6)
combine <- c(a, b)
combine[1] 1 2 3 4 5 6
temperatures <- c(15, 18, 20, 22, 19, 17, 16)
temperatures[c(2, 4, 6)][1] 18 22 17
temperatures[-5][1] 15 18 20 22 17 16
prices <- c(10, 20, 30, 40, 50)
prices * 0.9[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,…).
vec_numby5 <- seq(from = 5, to = 50, by =5)
vec_numby5 [1] 5 10 15 20 25 30 35 40 45 50
Random Patterns: Simulate the results of rolling a 6-sided die 10 times.
dice <- sample(1:6, size = 10, replace = TRUE)
dice [1] 2 1 6 2 6 3 1 3 3 4
Character Vector Onerations: Given the vector fruits <- c(“apple”, “banana”, “cherry”). extract the first three letters of each fruit.
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 vector are less than or equal to the corresponding elements in vector2.
vector1 <- c(5, 10, 15)
vector2 <- c(10, 10, 20)
vector1 <= vector2[1] TRUE TRUE TRUE