as.tibble()、as_data_frame() 和 tbl_df() 之间有什么区别?
What is the difference between as.tibble(), as_data_frame(), and tbl_df()?
我记得在某处读到 as.tibble()
是 as_data_frame()
的 别名 ,但我不知道 别名到底是什么 是编程术语。它类似于包装器吗?
所以我想我的问题可能归结为 tbl_df()
和 as_data_frame()
之间可能的用法差异:它们之间有什么区别,如果有的话?
更具体地说,给定一个(非 tibble)数据框 df
,我经常使用以下方法将它变成一个 tibble:
df <- tbl_df(df)
不会
df <- as_data_frame(df)
做同样的事情?如果是这样,是否还有其他情况下 tbl_df()
和 as_data_frame()
这两个函数不能互换使用以获得相同的结果?
tbl_df()
forwards the argument to as_data_frame()
这是否意味着 tbl_df()
是 as_data_frame()
的包装器或别名? R 文档似乎没有提到 as.tibble()
的任何内容,我忘记了我在哪里读到它是 as_data_frame()
的别名。此外,apparently as_tibble()
是 as_data_frame()
.
的另一个别名
如果这四个函数真的都是同一个函数,给一个函数起四个不同的名字有什么意义呢?这不是比帮助更令人困惑吗?
回答你 "whether it is confusing" 的问题,我想是的:) .
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>
但当然它调用相同的函数,所以它们是 "the same",或者如您所说的别名。
现在,我们可以看看泛型方法 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
对象。
根据 introduction to tibble,tibbles 似乎取代了 tbl_df
。
I’m pleased to announce tibble, a new package for manipulating and printing data frames in R. Tibbles are a modern reimagining of the data.frame, keeping what time has proven to be effective, and throwing out what is not. The name comes from dplyr: originally you created these objects with tbl_df()
, which was most easily pronounced as “tibble diff”.
[...]This package extracts out the tbl_df class associated functions from dplyr.
为了增加混乱,tbl_df
现在调用 as_tibble
,这是 as_data_frame
和 as.tibble
的首选别名:(Hadley Wickham's comment on the issue, and as_tibble docs)
> tbl_df
function (data)
{
as_tibble(data, .name_repair = "check_unique")
}
根据tbl_df()
的帮助说明,已弃用,应使用tibble::as_tibble()
代替。 as_data_frame
和 as.tibble
帮助页面都重定向到 as_tibble
.
在 tibble 上调用 class
时,class 名称仍显示为 tbl_df
:
> as_tibble(mtcars) %>% class
[1] "tbl_df" "tbl" "data.frame"
我记得在某处读到 as.tibble()
是 as_data_frame()
的 别名 ,但我不知道 别名到底是什么 是编程术语。它类似于包装器吗?
所以我想我的问题可能归结为 tbl_df()
和 as_data_frame()
之间可能的用法差异:它们之间有什么区别,如果有的话?
更具体地说,给定一个(非 tibble)数据框 df
,我经常使用以下方法将它变成一个 tibble:
df <- tbl_df(df)
不会
df <- as_data_frame(df)
做同样的事情?如果是这样,是否还有其他情况下 tbl_df()
和 as_data_frame()
这两个函数不能互换使用以获得相同的结果?
tbl_df()
forwards the argument toas_data_frame()
这是否意味着 tbl_df()
是 as_data_frame()
的包装器或别名? R 文档似乎没有提到 as.tibble()
的任何内容,我忘记了我在哪里读到它是 as_data_frame()
的别名。此外,apparently as_tibble()
是 as_data_frame()
.
如果这四个函数真的都是同一个函数,给一个函数起四个不同的名字有什么意义呢?这不是比帮助更令人困惑吗?
回答你 "whether it is confusing" 的问题,我想是的:) .
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>
但当然它调用相同的函数,所以它们是 "the same",或者如您所说的别名。
现在,我们可以看看泛型方法 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
对象。
根据 introduction to tibble,tibbles 似乎取代了 tbl_df
。
I’m pleased to announce tibble, a new package for manipulating and printing data frames in R. Tibbles are a modern reimagining of the data.frame, keeping what time has proven to be effective, and throwing out what is not. The name comes from dplyr: originally you created these objects with
tbl_df()
, which was most easily pronounced as “tibble diff”.[...]This package extracts out the tbl_df class associated functions from dplyr.
为了增加混乱,tbl_df
现在调用 as_tibble
,这是 as_data_frame
和 as.tibble
的首选别名:(Hadley Wickham's comment on the issue, and as_tibble docs)
> tbl_df
function (data)
{
as_tibble(data, .name_repair = "check_unique")
}
根据tbl_df()
的帮助说明,已弃用,应使用tibble::as_tibble()
代替。 as_data_frame
和 as.tibble
帮助页面都重定向到 as_tibble
.
在 tibble 上调用 class
时,class 名称仍显示为 tbl_df
:
> as_tibble(mtcars) %>% class
[1] "tbl_df" "tbl" "data.frame"