要回答您“是否令人困惑”的问题,我想是这样的:)。
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
如果您查看code for 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 取消分组传递的数据帧。
总的来说,as_data_frame 似乎应该被视为提供了比as_tibble 更多的功能,除非您正在处理ts 或poly 对象。