Classwork 4

Filtering a Vector

Author

Byeong-Hak Choe

Published

September 25, 2026

Question 1.

  • 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.
  • Use positional indexing to keep the beer names in positions 2, 4, and 6.

Answer:



Question 2.

  • 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.
  • Use logical_vec inside square brackets to keep only the ages that are greater than or equal to 20.
ages <- c(15, 22, 18, 24, 30)

Answer:



Back to top