R Basics

You can insert an R code chunk into an R Markdown document by using a keyboard shortcut. The default keyboard shortcut to insert an R code is:

Windows/Linux: Ctrl + Alt + I Mac: Command + Option + I

Make sure to comment on your code using #

Problem 1:

  1. From set = {1.5, 15, 0, 54, 23}, pick two numbers and perform the Following Operations:

.Subtraction .Multiplication .square root

# Defined the set of numbers provided above 
number_set <- c(1.5,15,0,54,23)

# selected two numbers ( 4th element is 54, and 2nd element is 15)
a <- number_set[4]
b <- number_set[2]

# 1. Subtraction
subtraction_result <- a - b
print(paste("Subtraction:", subtraction_result))
## [1] "Subtraction: 39"
# 2.Multiplication
multiplication_result <- a * b
print(paste("Multiplication:", multiplication_result))
## [1] "Multiplication: 810"
# 3. Square Root
sqrt_a <- sqrt(a)
sqrt_b <- sqrt(b)
print(paste("Square Root of:", a, "is:", round(sqrt_a, 2))) 
## [1] "Square Root of: 54 is: 7.35"
print(paste("Square Root of:", b, "is:", round(sqrt_b, 2))) 
## [1] "Square Root of: 15 is: 3.87"

Problem 2

Suppose you have a set of numbers representing the assignments scores:

# Define the vector of scores 
scores <- c(87, 100, 91, 95, 81.5, 0, 39, 74, 92)

# 1. Calculate the average of the scores, and round your answer to two decimal number
avg_score <- round(mean(scores), 2)
print (paste("Average score:", avg_score))
## [1] "Average score: 73.28"
# 2. Find the Maximum and Minimum score
max_score <- max(scores)
min_score <- min(scores)
print(paste("Maximum score:", max_score))
## [1] "Maximum score: 100"
print(paste("minimum score:", min_score))
## [1] "minimum score: 0"
# 3. Find how many scores were above the average, use coding for this
scores_above_avg <- sum(scores > mean(scores))
print(paste("Number of scores above average:", scores_above_avg))
## [1] "Number of scores above average: 7"
# 4. Create a new set called new_scores that includes the elements from indices 2 to 6 of the scores vector
new_scores <- scores[2:6]
# Round the vector to 0 decimal
rounded_scores <- round(new_scores, digits = 0)
print("New scores vector:")
## [1] "New scores vector:"
print (rounded_scores)
## [1] 100  91  95  82   0

Problem 3:

Read section 2.3, then answer the following questions

# 1. Create a vector with the number 1 repeated 5 times. Print it
ones_vector <- rep(1, times = 5)
print(ones_vector)
## [1] 1 1 1 1 1
# 2. Create a vector with a sequence of numbers from 1 to 10 with a step of 2. Print it
sequence_vector <- seq(from = 1, to = 10, by = 2)
print(sequence_vector)
## [1] 1 3 5 7 9
# 3. Create a vector by repeating a sequence from 1 to 3, 2 times. Print it.
repeated_sequence <- rep(1:3, times = 2)
print(repeated_sequence)
## [1] 1 2 3 1 2 3

Problem 4

mixed_vector <- c(1, "two", 3.0, TRUE, 2)

# 1. Check the class of the vector
vector_class <- class(mixed_vector)
print(vector_class)
## [1] "character"
# 2. Coerce to numeric and print it 
numeric_vector <- as.numeric(mixed_vector)
## Warning: NAs introduced by coercion
print(numeric_vector)
## [1]  1 NA  3 NA  2