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
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
numerical
values> nums <- c(1,2,3,4,5,6,7)
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
[1] 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
[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE
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
[1] 2 4 6
And there we have done the Logical Indexing.