my_vec <- c(-10, -20, 30, 10, 50, 40, -100)
beers <- c("BUD LIGHT", "BUSCH LIGHT", "COORS LIGHT",
"GENESEE LIGHT", "MILLER LITE", "NATURAL LIGHT")Classwork 4
Filtering a Vector
Question 1.
- Consider the vectors:
- 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_vecthat checks whether the elements of the vectorages <- c(15, 22, 18, 24, 30)are greater than or equal to 20. - Use
logical_vecinside square brackets to keep only the ages that are greater than or equal to 20.
ages <- c(15, 22, 18, 24, 30)Answer:
