Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of %in%: exclude rows with values specified in a vector

A categorical variable V1 in a data frame D1 can have values represented by the letters from A to Z. I want to create a subset D2, which excludes some values, say, B, N and T. Basically, I want a command which is the opposite of %in%

D2 = subset(D1, V1 %in% c("B", "N", "T")) 
like image 386
user702432 Avatar asked Apr 29 '11 12:04

user702432


People also ask

What is the opposite of in in R?

R operator %in% is handy for working with vectors, but how to use it oppositely? Something like %notin% that will exclude anything that is in a vector. There is no actual %notin% operator in R, but below is the explanation on how to get the desired result.

How do I exclude values from a dataset in R?

To exclude variables from dataset, use same function but with the sign - before the colon number like dt[,c(-x,-y)] . Sometimes you need to exclude observation based on certain condition. For this task the function subset() is used.

How do I specify not in R?

You can use the following basic syntax to select all elements that are not in a list of values in R: ! (data %in% c(value1, value2, value3, ...))

Is there a NOT IN operator in R?

The not in r is the Negation of the %in% operator. The %in% operator is used to identify if an element belongs to a vector. The ! indicates logical negation (NOT).


1 Answers

You can use the ! operator to basically make any TRUE FALSE and every FALSE TRUE. so:

D2 = subset(D1, !(V1 %in% c('B','N','T'))) 

EDIT: You can also make an operator yourself:

'%!in%' <- function(x,y)!('%in%'(x,y))  c(1,3,11)%!in%1:10 [1] FALSE FALSE  TRUE 
like image 71
Sacha Epskamp Avatar answered Sep 20 '22 02:09

Sacha Epskamp