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"))
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.
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.
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, ...))
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With