为什么 `data.table::unique` 不起作用? [英] Why `data.table::unique` doesn't work?

查看:22
本文介绍了为什么 `data.table::unique` 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

help(unique) 表明 unique 函数存在于两个包中 - basedata.table.我想使用 data.table 包中的这个函数.我认为以下语法 - data <- data.table::unique(data) 表示要使用的包.但我收到以下错误 -

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' 不是从 'namespace:data.table' 导出的对象

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

但是 data <- unique(data) 效果很好.

这里有什么问题?

推荐答案

有问题的函数真的是unique.data.tabledata.table包.该方法并不真正打算直接调用,因此不会导出.这通常是 S3 方法的情况.相反,包将该方法注册为 S3 方法,然后允许 S3 泛型 base::unique 在这种情况下调度它.所以调用函数的正确方式是:

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)

我们使用导出的base::unique,调度不导出的data.table:::unique.data.table.data.table:::unique 函数实际上并不存在(或者它是否需要).

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).

正如 eddi 指出的那样,base::unique 基于被调用对象的类进行调度.所以 base::unique 只有当对象是 data.table 时才会调用 data.table:::unique.data.table.您可以使用 data.table:::unique.data.table(iris) 之类的方式直接强制调用该方法,但在内部这很可能会导致调用下一个方法,除非您的对象实际上是一个data.table.

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.

这篇关于为什么 `data.table::unique` 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆