Given the set of numbers S = {2,5, 15, 0, 54, 23}, follow these steps:
#1.1 Find the Range: Subtract the smallest value from the largest value in the set.
54 - 0
## [1] 54
#1.2 Multiply the first odd number in the set by 4.
5 * 4
## [1] 20
#1.3 Square Root: Find the square root of the largest value (if applicable).
sqrt(54)
## [1] 7.348469
Suppose you have a set of temperatures (in °C) recorded over the past few days:
Follow these steps to analyze the data:
temperatures <- c(12, 18, 25, 30, 27, 14, 19, 22, 28)
# 2.1 Calculate the average temperature, rounded to 2 decimal places
round(mean(temperatures), 2)
## [1] 21.67
# 2.2 Find the maximum and minimum temperature
max(temperatures)
## [1] 30
min(temperatures)
## [1] 12
# 2.3 Count how many temperatures are above the average
avg_temp <- mean(temperatures)
sum(temperatures > avg_temp)
## [1] 5
# 2.4 Create a new set of temperatures from indices 3 to 7
subset_temps <- temperatures[3:7]
subset_temps
## [1] 25 30 27 14 19
Read section 2.3, then answer the following questions
# 3.1 Create a vector with the number 1 repeated 5 times. Print it
rep(1, 5)
## [1] 1 1 1 1 1
# 3.2 Create a vector with a sequence of numbers from 1 to 10 with a step of 2. Print it
seq(1, 10, by = 2)
## [1] 1 3 5 7 9
# 3.3 Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.
rep(1:3, times = 2)
## [1] 1 2 3 1 2 3
mixed_vector4 <- c(1,7,"Now", "Find", 5.0, TRUE, 6)
# 4.1 Check the class of the vector
class(mixed_vector4)
## [1] "character"
# 4.2 Coerce to numeric and print it
as.numeric(mixed_vector4)
## Warning: NAs introduced by coercion
## [1] 1 7 NA NA 5 NA 6
# 4.3 Given two boolean vectors, perform the OR and AND operations.
vector_b1 <- c(TRUE, FALSE, TRUE, FALSE)
vector_b2 <- c(FALSE, FALSE, TRUE, TRUE)
# Perform OR operation
vector_b1 | vector_b2
## [1] TRUE FALSE TRUE TRUE
# Perform AND operation
vector_b1 & vector_b2
## [1] FALSE FALSE TRUE FALSE
# Print the results