回答你的问题,“是否令人困惑”,我认为是的 :)。
as.tibble
和
as_tibble
是相同的;两者只是简单调用S3方法
as_tibble
:
> as.tibble
function (x, ...)
{
UseMethod("as_tibble")
}
<environment: namespace:tibble>
as_data_frame
和tbl_df
并不完全相同;tbl_df
会调用as_data_frame
:
> tbl_df
function (data)
{
as_data_frame(data)
}
<environment: namespace:dplyr>
注意:tbl_df
在 dplyr
中,而 as_data_frame
在 tibble
包中:
> as_data_frame
function (x, ...)
{
UseMethod("as_data_frame")
}
<environment: namespace:tibble>
但是当然它调用了相同的函数,因此它们是“相同的”,或者正如你所说的别名。
现在,我们可以看一下通用方法 as_tibble
和 as_data_frame
之间的区别。首先,我们看一下每个方法的特点:
> methods(as_tibble)
[1] as_tibble.data.frame* as_tibble.default* as_tibble.list* as_tibble.matrix* as_tibble.NULL*
[6] as_tibble.poly* as_tibble.table* as_tibble.tbl_df* as_tibble.ts*
see '?methods' for accessing help and source code
> methods(as_data_frame)
[1] as_data_frame.data.frame* as_data_frame.default* as_data_frame.grouped_df* as_data_frame.list*
[5] as_data_frame.matrix* as_data_frame.NULL* as_data_frame.table* as_data_frame.tbl_cube*
[9] as_data_frame.tbl_df*
see '?methods' for accessing help and source code
如果您查看
as_tibble 的代码,您会发现许多
as_data_frame
方法的定义。
as_tibble
定义了两个额外的方法,而这两个方法并没有为
as_data_frame
定义,它们分别是
as_tibble.ts
和
as_tibble.poly
。我不太确定为什么它们也不能为
as_data_frame
定义。
as_data_frame
还有两个额外的方法,它们都在
dplyr
中定义:
as_data_frame.tbl_cube
和
as_data_frame.grouped_df
。
as_data_frame.tbl_cube
使用更弱的
as.data.frame
检查(请耐心等待),然后调用
as_data_frame
:
> getAnywhere(as_data_frame.tbl_cube)
function (x, ...)
{
as_data_frame(as.data.frame(x, ..., stringsAsFactors = FALSE))
}
<environment: namespace:dplyr>
当 as_data_frame.grouped_df
取消分组的时候。
总体而言,除非您处理 ts
或 poly
对象,否则应将 as_data_frame
视为提供比 as_tibble
更多的功能。
functionName
(注意没有括号)、methods(functionName)
、getAnywhere(functionName.classMethodName)
以及直接查看tibble
和dplyr
源代码的组合。 - rsmith54as_data_frame
已经被软弃用,只是调用as_tibble
(它不再是通用的),tbl_df
调用as_tibble(data, .name_repair = "check_unique")
。as.tibble
已经被软弃用。 - moodymudskipperas_tibble
现在也会抛出弃用警告。 - Simon Woodward