Classwork 5

R Basics II

Author

Byeong-Hak Choe

Published

September 26, 2025

Modified

September 25, 2025

Question 1.

  • Write an R code to calculate the standard deviation (SD) of the integer vector x below manually. That is to calculate the SD without using the sd() or the var() functions.
x <- 1:25
  • Also, write an R code to test whether the standard deviation you calculate manually above is equal to sd(x).

Answer:



Question 2.

  • Write an R code to calculate the range (difference between the maximum and minimum values) of the vector temp <- c(22, 28, 31, 25, 29).
temp <- c(22, 28, 31, 25, 29)

Answer:



Question 3.

  • Consider the vectors:
my_vec <- c(-10, -20, 30, 10, 50, 40, -100)
beers <- c("BUD LIGHT", "BUSCH LIGHT", "COORS LIGHT", 
           "GENESEE LIGHT", "MILLER LITE", "NATURAL LIGHT")
  • Write an R code to filter only the positive values in my_vec.
  • Write an R code to access the beers that are in positions 2, 4, and 6 using indexing.

Answer:



Question 4.

  • Create a logical vector logical_vec that checks whether the elements of the vector ages <- c(15, 22, 18, 24, 30) are greater than or equal to 20.
ages <- c(15, 22, 18, 24, 30)

Answer:



Back to top