# Check if Number is Odd or Even in R (2 Examples)

In this tutorial, Iâ€™ll demonstrate how to **find odd and even numbers** in the R programming language.

Table of contents:

So now the part you have been waiting for â€“ the examples:

## Creation of Exemplifying Data

The following data is used as a basis for this R programming language tutorial:

x <- 1:5 # Create example vector x # [1] 1 2 3 4 5

The previous output of the RStudio console shows that our example data is a vector object containing five different integer values (i.e. a range from 1 to 5).

## Example 1: Find Even Numbers in Vector

Example 1 explains how to get all even elements of a vector object.

For this task, we can use the %% and == operators as shown below:

x_logical <- x %% 2 == 0 # Create even/odd logical x_logical # Print even/odd logical # [1] FALSE TRUE FALSE TRUE FALSE

The previous syntax has created a new logical data object called x_logical, which identifies the even and odd numbers in our vector object.

We can now use this logical indicator to subset our vector object x:

x_even <- x[x_logical] # Extract even numbers x_even # Print even numbers # [1] 2 4

The previous R syntax has returned the values 2 and 4, i.e. the even numbers in our data.

## Example 2: Find Odd Numbers in Vector

Similar to Example 1, we can also extract the odd numbers from a data object.

For this task, we have to specify the ! operator in front of the logical indicator:

x_odd <- x[!x_logical] # Extract odd numbers x_odd # Print odd numbers # [1] 1 3 5

The previous R code has selected only the odd values in our vector.

## Video, Further Resources & Summary

Have a look at the following video instruction which I have published on the Statistics Globe YouTube channel. Iâ€™m explaining the R programming syntax of this tutorial in the video.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

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.

- Check if Column is Date in R
- Select Odd & Even Rows & Columns from Data Frame
- Check if Package is Missing and Install Automatically (R Example)
- Check if Number is Integer in R
- All R Programming Examples

This tutorial has illustrated how to **check for odd and even numbers** in R programming.

Note that we have shown an example based on a vector object in the present tutorial. However, we could apply the same type of syntax to return odd and even values from a data frame column.

Let me know in the comments below, in case you have additional questions.

### Statistics Globe Newsletter