emp <- data.frame(
name = c("ram", "prabhu", "tina", "venkatesan"),
dept = c("IT", "HR", "compliance", "operations")
)
emp
## name dept
## 1 ram IT
## 2 prabhu HR
## 3 tina compliance
## 4 venkatesan operations
retired <- data.frame(name = c("tina",
"prasath"))
retired
## name
## 1 tina
## 2 prasath
emp$name %in% retired$name
## [1] FALSE FALSE TRUE FALSE
which(emp$name %in% retired$name)
## [1] 3
emp[emp$name %in% retired$name, ]
## name dept
## 3 tina compliance
tryCatch(
{
emp[emp$name %notin% retired$name, ] #such an operator does not exist
}, error = function(e) {
paste(e, "is handled.")
}
)
## [1] "Error in `[.data.frame`(emp, emp$name %notin% retired$name, ): could not find function \"%notin%\"\n is handled."
`%notin%` <- #create a new binary pipe operator
function (x, table)
is.na(match(x, table, nomatch = NA_integer_))
emp[emp$name %notin% retired$name, ]
## name dept
## 1 ram IT
## 2 prabhu HR
## 4 venkatesan operations