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:
# Define the numbers
a <- 15
b <- 1.5

# Subtraction
subtraction_result <- a - b
subtraction_result
## [1] 13.5
# Multiplication
multiplication_result <- a * b
multiplication_result
## [1] 22.5
# Square root (apply to one chosen number)
sqrt_result <- sqrt(a)
sqrt_result
## [1] 3.872983

Problem 2

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

scores <- c(87,100, 91, 95, 81.5, 0, 39, 74, 92)

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

# Calculate the average of the scores, round to two decimal places
average_score <- round(mean(scores), 2)
average_score
## [1] 73.28
# Find the Maximum and Minimum score
max_score <- max(scores)
min_score <- min(scores)

max_score
## [1] 100
min_score
## [1] 0
# Find how many scores were above the average
above_average_count <- sum(scores > average_score)
above_average_count
## [1] 7
# Create a new set called new_scores that includes elements from indices 2 to 6
new_scores <- scores[2:6]
new_scores
## [1] 100.0  91.0  95.0  81.5   0.0

Problem 3:

Read section 2.3, then answer the following questions

# Create a vector with the number 1 repeated 5 times
vector1 <- rep(1, 5)
vector1
## [1] 1 1 1 1 1
# Create a vector with a sequence of numbers from 1 to 10 with a step of 2
vector2 <- seq(1, 10, by = 2)
vector2
## [1] 1 3 5 7 9
# Create a vector by repeating a sequence from 1 to 3, 2 times
vector3 <- rep(1:3, times = 2)
vector3
## [1] 1 2 3 1 2 3

Problem 4

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

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