# %in% operator in R

%in% operator in R, 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.

• 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.

#### Example of %in% operator in R for Vectors

```# 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

 TRUE

```# R %in% operator

V2 %in% t
```

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

 FALSE

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

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

```# 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

 FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE

#### Create column using %in% operator in R: 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
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))

```

so the resultant dataframe will be now lets use %in% operator to create a new variable

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

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'
})

```

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. #### Drop column using %in% operator dropping a column can also accomplished using %in% operator as shown below.

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

```

we will be dropping a column name that has “Tax”, so the resultant table will be #### 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 