Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
What are vectors?
Vector operations
Indexing and filtering
16. Vector filtering
Summary

Instruction

Great! A logical vector can be used to perform vector filtering.

For example, if we want to extract from ages only those values that are less than 35, we can use the following statement:

ages[ages < 35]

The expression begins with the name of the vector that we want to filter (ages), followed by the bracket operator. Inside the brackets, we now define a comparison. This will return  TRUE/FALSE values for each element of the vector ages. In our example, the comparison will return the following:

c(TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE)

Elements in ages for which the corresponding comparison value is TRUE will be extracted from the vector. If a vector element's corresponding comparison value is FALSE, it will not be retrieved:

ages   
[1] 28 29 43 39 31 29 28 34 31 24 22 25
ages[ages < 35]
[1] 28 29 31 29 28 34 31 24 22 25

Exercise

Retrieve salaries that are greater than 5000 dollars from the vector named salaries.