Why `data.table::unique` doesn't work?

2020-02-14 18:36发布

help(unique) shows that unique function is present in two packages - base and data.table. I would like to use this function from data.table package. I thought that the following syntax - data <- data.table::unique(data) indicates the package to be used. But I get the following error -

'unique' is not an exported object from 'namespace:data.table'

But data <- unique(data) works well.

What is wrong here?

2条回答
叼着烟拽天下
2楼-- · 2020-02-14 18:41

There are actually two infix operators in R that pull functions from particular package namespaces. You used :: but there is also a ::: that retrieves "unexported" functions. The unique-function is actually a family of functions and its behavior will depend on both the class of its argument and the particular packages that have been loaded. The R term of this is "generic". Try:

data <- data.table:::unique(data)  # assuming 'data' is a data.table

The other tool that lets you peek behind the curtain that the lack of "exportation" is creating is the getAnywhere-function. It lets you see the code at the console:

> unique.data.table
Error: object 'unique.data.table' not found

> getAnywhere(unique.data.table)
A single object matching ‘unique.data.table’ was found
It was found in the following places
  registered S3 method for unique from namespace data.table
  namespace:data.table
with value

function (x, incomparables = FALSE, fromLast = FALSE, by = key(x), 
    ...) 
{
    if (!cedta()) 
        return(NextMethod("unique"))
    dups <- duplicated.data.table(x, incomparables, fromLast, 
        by, ...)
    .Call(CsubsetDT, x, which_(dups, FALSE), seq_len(ncol(x)))
}
<bytecode: 0x2ff645950>
<environment: namespace:data.table>
查看更多
小情绪 Triste *
3楼-- · 2020-02-14 19:04

The function in question is really unique.data.table, an S3 method defined in the data.table package. That method is not really intended to be called directly, so it isn't exported. This is typically the case with S3 methods. Instead, the package registers the method as an S3 method, which then allows the S3 generic, base::unique in this case, to dispatch on it. So the right way to call the function is:

library(data.table)
irisDT <- data.table(iris)
unique(irisDT)

We use base::unique, which is exported, and it dispatches data.table:::unique.data.table, which is not exported. The function data.table:::unique does not actually exist (or does it need to).

As eddi points out, base::unique dispatches based on the class of the object called. So base::unique will call data.table:::unique.data.table only if the object is a data.table. You can force a call to that method directly with something like data.table:::unique.data.table(iris), but internally that will mostly likely result in the next method getting called unless your object is actually a data.table.

查看更多
登录 后发表回答