usage of %in% operator in R

%in% operator in R along with pipe operator is used for multiple purpose, one of its usage is that it is used to identify if an element belongs to a vector or Dataframe. Let see an example on how to use the %in% operator for vector and Dataframe in R.

  • Use %in% operator to Check value in a vector.
  • Use %in% operator to Check if one vector present in another vector.
  • Check if any value in of one vector present in another vector.
  • Check if all value in one vector present in another vector.
  • Filter dataframe using %in% operator in R
  • select column of a dataframe in R using %in% operator.
  • create new variable of a column using %in% operator
  • drop column of a dataframe in R using %in% operator.

 

USAGE of %in% Operator in R Vector:

Example of %in% operator in R for Vectors

Check if a value is present in the vector . Check Is value v1 present in the vector t.

 
# 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 

 

Check if one vector present in another vector:

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
vec2 <- c(1,2,5,34) 
vec2 %in% vec1

Check Vector vec2 is present in the vector vec1, so the output will be

[1]  TRUE FALSE  TRUE FALSE

 

 

Check if any value in of one vector present in another vector:

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
vec2 <- c(1,2,5,34) 
any(vec2 %in% vec1)

Check if any value of Vector vec2 is present in the vector vec1, so the output will be

[1]  TRUE

 

 

 

Check if all value in of one vector present in another vector:

Example 1:

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
vec2 <- c(1,2,5,34) 
all(vec2 %in% vec1)

Check if all values of  vector vec2 is present in the vector vec1, so the output will be

[1]  FALSE 

 

Example 2:

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
vec2 <- c(1,5,34) 
all(vec2 %in% vec1)

Check if all values of  vector vec2 is present in the vector vec1, so the output will be

[1] TRUE

 

 

Get the Index of the Vector using %in% operator in R:

Example 1:

To Get the index of the vector in R, we use %in% operator along with which() function.

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
which(vec1 %in% 13)

which() function will take the value and returns the position of  value in the vector vec1, so the output will be

[1] 5

 

Example 2:

To Get the index of the vector, we use %in% operator along with which() function.

# R %in% operator
vec1 <- c(1,3,4,5,13,33,79,43)
which(vec1 %in% 4)

which() function will take the value and returns the position of  the value in vector vec1, so the output will be

[1] 3

 

 

 

USAGE of in Operator in R Dataframe:

Create column using %in% operator in R:

%in% operator in R using dplyr in R 15

 

Lets create my_basket dataframe in order to depict an example of create column using %in% operator and some other operations

### Create dataframe in R
my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,60),
                       Tax = c(2,4,5,NA,2,3,NA,1,NA,4,5,NA,4,NA))

my_basket

so the resultant dataframe will be

%in% operator in R using dplyr in R 11

now lets use %in% operator to create a new variable

### Create a new variable using %in% operator in R

my_basket1=within(my_basket,{
IS_DAIRY='NO'
IS_DAIRY[ITEM_NAME %in% c('Milk',"Curd","Cheese","Paneer")]='YES'
IS_DAIRY[ITEM_GROUP %in% c("Dairy")]='YES'
IS_DAIRY[ITEM_GROUP %in% c("Fruit","Vegetable")]='NO'
})

my_basket1

In order to create a new variable namely “IS_DAIRY“, we will be using %in% operator which will assign YES if the ITEM_GROUP  is Dairy or ITEM_NAME is Dairy products. Else it will assign NO.

%in% operator in R using dplyr in R 12

 

 

Filter dataframe using %in% operator 

### Filter a dataframe using %in% operator in R

df_filter<- my_basket [my_basket$ITEM_GROUP  %in% c('Fruit'), ]
df_filter

In order to perform filter we will be using in operator and stored into a new dataframe. using %in% operator which will filter Fruit in ITEM_GROUP Column. so the resultant dataframe will be

In operator in R

 

 

Drop column using %in% operator 

%in% operator in R using dplyr in R 14

dropping a column can also accomplished using %in% operator as shown below.

### Drop column using %in% operator in R

my_basket[, !(colnames(my_basket) %in% c("Tax"))]

we will be dropping a column name that has “Tax”, so the resultant table will be

%in% operator in R using dplyr in R 13

 

 

select columns using %in% operator 

selecting column names can also accomplished using %in% operator,  as shown below. which selects the list of column names using select_if() function along with pipe(%>%) and %in% operator.

### select columns using %in% operator in R

my_basket %>% 
  select_if(names(.) %in% c('Price', 'ITEM_GROUP', 'ITEM_NAME'))

we will be selecting some column names , so the resultant table will be

%in% operator in R using dplyr in R 13

 


Other Related Topics:

 

 

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

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts