Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of %in% in r [duplicate]

Tags:

r

Possible Duplicate:
Opposite of %in%

What is the opposite of

matrix[matrix%in%1,]?

!%in% does not work.

I would like to select items that do not contain a certain number.

like image 317
user1723765 Avatar asked Dec 31 '12 19:12

user1723765


People also ask

What is opposite of in in r?

R %not in% operator, opposite to %in%

How do you say not in 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, ...))

How do I exclude a subset in R?

To select variables from a dataset you can use this function dt[,c("x","y")] , where dt is the name of dataset and “x” and “y” name of vaiables. To exclude variables from dataset, use same function but with the sign - before the colon number like dt[,c(-x,-y)] .


2 Answers

If you find yourself using @Joshua's suggestion often, you could easily make your own %notin% operator.

`%notin%` <- Negate(`%in%`)
'a' %notin% c('b', 'c')
# [1] TRUE
like image 135
Matthew Plourde Avatar answered Oct 02 '22 13:10

Matthew Plourde


You want:

matrix[!matrix %in% 1,]

For clarity's sake, I prefer this, even though the parentheses aren't necessary.

matrix[!(matrix %in% 1),]

Also note that you need to be aware of FAQ 7.31: Why doesn't R think these numbers are equal?.

like image 28
Joshua Ulrich Avatar answered Oct 02 '22 13:10

Joshua Ulrich