How to use %in% operator in R
How to use %in% operator in R?, Want to know for certain whether a value is included within an R vector quickly? You are probably seeking the R’s percent in percent operator.
How to use %in% operator in R
If the value is present in the array you’re examining, it takes a single value and produces a Boolean True / False result. Below is a sample of code.
%in% in r example
x<- c(11,22,33,44,55,63,12,44,32,27,12) x
[1] 11 22 33 44 55 63 12 44 32 27 12
%in% in r example missing value
31 %in% x
[1] FALSE
%in% in r example without missing value
44 %in% x
[1] TRUE
%in% r – using variables for search value and vector
value <- 12 value %in% x
[1] TRUE
%in% R applied to check Data Frames
To determine whether a specific value is present in a column of an R data frame, the same operator can be used.
In the example that follows, we’ll examine some time stamps from the ChickWeight data set, which is one of R’s built-in data sets and shows how quickly hens grow on various diets.
This analysis’s (fictitious) objective is to determine whether there are data points available for particular days.
head(ChickWeight)
weight Time Chick Diet 1 42 0 1 1 2 51 2 1 1 3 59 4 1 1 4 64 6 1 1 5 76 8 1 1 6 93 10 1 1
%in% in r – no value
5 %in% ChickWeight$Time
[1] FALSE
%in% in r, with value
8 %in% ChickWeight$Time
[1] TRUE
Comparing two vectors using the percent in percent operator
The percent in percent operator can be used to compare two vectors.
It will return an array with the same number of elements as the initial vector, each of which will indicate whether an element is present or absent (True or False).
This is a useful method for handling complicated comparisons fast where there may be several conditions on either side of the logic statement.
%in% in R example – comparing vectors
check vector of multiple values – %in% r operator
c(10, 11) %in% x [1] FALSE TRUE
Why put a percent sign before a percentage?
There are several options available here, as there are in most other areas of the R programming language.
Programmers frequently need to determine if a value is present or absent in an array. Having said that, we appreciate how elegantly simple and easily readable the percent in percent operator is.
It is believed that software should not be developed for computers to process, but rather for people to read.
The percent in percent operator realizes this ambition with its exquisite simplicity and easy usability.
You can find additional information here pipe operator in R-Simplify Your Code with %>% »
Have you found this article to be interesting? We’d be glad if you could forward it to a friend or share it on Twitter or Linked In to help it spread.