# Need help with logical indexing topic with suitable examples

Hi All,

Hope you all are doing well.

I am facing difficulty understand logical indexing in vectors in “R” data analyst course.

Can someone help me with the same with suitable examples.

Thanks,
Nishant

1. Let’s start by creating a vector holding `numerical` values
``````> nums <- c(1,2,3,4,5,6,7)
``````
1. From our `nums` vector, which items are divisible by `2`?

We know that any number that is divisible by 2 the remainder is zero (`0`)

``````> nums %% 2 == 0
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
``````

Wow!!, we’ve got an output of logicals that tells us if a number is divisible by 2 or not. So lets create another vector that holds these `Logicals`.
3.

``````> logics <- nums %%2 == 0
> logics
 FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
``````
1. Ok, now we have a vector with logicals, can we use the `logics` as a logical index to get numbers divisible by `2`? Of Course, Lets do this…

a number divisible by 2 is known as an Even Number

``````> even_nums <- nums[logics]
> even_nums
 2 4 6
``````

And there we have done the Logical Indexing.