0

I am trying to define the package for a specific function starting with %. For example let's take the %nin% function from the Hmisc package:

df1 <- "id name
       1   one
       2   two"

df1 <- read.table(text=df1, header=TRUE)

df2 <- "id name
       3   three
       2   two"

df2 <- read.table(text=df2, header=TRUE)

library(Hmisc)
df1[which(df1$id %nin% df2$id),]

Then specifying the package without loading it I get an error:

   df1[which(df1$id Hmisc::%nin% df2$id),]

Error: unexpected symbol in "df1[which(df1$id Hmisc"

Any idea how to do it correctly?

user3091668
  • 2,230
  • 6
  • 25
  • 42
  • `df1[which(df1$id Hmisc::`%nin%` df2$id),]` does not work. Moreover, the question you refer as duplicate seems to address another problem – user3091668 May 25 '18 at 14:53
  • strange, it works to get the function definition. You can do ```Hmisc::`%nin%`(df1$id, df2$id)``` which works – Cath May 25 '18 at 14:55
  • Yes. Don't go through. Could you please take out the duplicated tag? The answers in the other post don't solve this specific problem. – user3091668 May 25 '18 at 14:58
  • btw you don't need to use which with logical, `df1[df1$id %nin% df2$id,]` gives the same – Cath May 25 '18 at 15:01
  • I need to include the package name because this is a function for a package and I don't want to import all functions from Hmisc only to use this one in a specific line – user3091668 May 25 '18 at 15:03
  • so ```df1[Hmisc::`%nin%`(df1$id, df2$id), ]``` (I just didn't want to go through all the backticks to get it printed ok ;-) ) – Cath May 25 '18 at 15:20

1 Answers1

4

You need to surround the call in backticks and use it as a "regular" function:

df1[Hmisc::`%nin%`(df1$id, df2$id), ]
#    id name
#   1  1  one

N.B.: To learn more on those kind of operators: R: What are operators like %in% called and how can I learn about them?

Cath
  • 23,906
  • 5
  • 52
  • 86