%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

[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

# 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] 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:

%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

 

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