%in% operator in R

%in% operator in R, is used to identify if an element belongs to a vector.

Example of %in% operator in R

# R %in% operator

v1 <- 3
v2 <- 101
t <- c(1,2,3,4,5,6,7,8)
v1 %in% t

In the above example v1 is present in t, so the output will be

[1] TRUE

 

 

# R %in% operator

V2 %in% t

V2 is not present in t, so the output will be

[1] FALSE

 

 

Example of %in% operator in R for data frame:

Lets use the mtcars data frame to depict an example of %in% operator in R

# R %in% operator for mtcars data frame

mtcars$cyl %in% 4

Wherever the cylinder value =4 it will return TRUE and if not 4 it will return FALSE so the output will be

[1][1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE

[20] TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE

previous small %in% operator in R                                                                                                                next small %in% operator in R