Find Odd & Even Numbers in R (2 Examples)
In this R programming tutorial you’ll learn how to subset odd and even numbers.
Creation of Example Data
vec <- c(1, 4, 7, 4, 5, 8) # Create example vector vec # [1] 1 4 7 4 5 8 |
vec <- c(1, 4, 7, 4, 5, 8) # Create example vector vec # [1] 1 4 7 4 5 8
Example 1: Extract Odd Numbers from Vector Object
vec[vec %% 2 != 0] # Subset odd values # [1] 1 7 5 |
vec[vec %% 2 != 0] # Subset odd values # [1] 1 7 5
Example 2: Extract Even Numbers from Vector Object
vec[vec %% 2 == 0] # Subset even values # [1] 4 4 8 |
vec[vec %% 2 == 0] # Subset even values # [1] 4 4 8
Further Resources & Related Tutorials
Furthermore, you may have a look at the related articles that I have published on this website. Some articles about similar topics such as numeric values, extracting data, and dates are listed below.