Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negate %like% in Data.Table Package | R

I am trying to use the %like% function from data.table pacakge but in reverse. I have not been able to find a way to negate the function. Any help will be much appreciate.

Would rather not use grepl with the invert = TRUE option. Trying to keep the code as simple as possible.

#Original code to find the match#

library(data.table)
Table1 <- data.table(Table1)
Table1 <-Table1[`Account Name` %like% 'Nike']
like image 916
Koolakuf_DR Avatar asked Oct 26 '25 19:10

Koolakuf_DR


2 Answers

We can use

Table1[!`Account Name` %like% 'Nike']
#   Account Name      Col2
#1:       Others 0.4196231

data

set.seed(24)
Table1 <- data.table(`Account Name` = c("Nike brand", "Nike shoes",
       "Others"), Col2 = rnorm(3))
like image 122
akrun Avatar answered Oct 29 '25 07:10

akrun


In case you wanted to create an actual "not like" function, you could do so as below

`%notlike%` <- Negate(`%like%`)

'something' %notlike% 'anotherthing'
# [1] TRUE
like image 26
IceCreamToucan Avatar answered Oct 29 '25 08:10

IceCreamToucan