as.tibble(),as_data_frame()和tbl_df()有什么区别? [英] What is the difference between as.tibble(), as_data_frame(), and tbl_df()?

查看:975
本文介绍了as.tibble(),as_data_frame()和tbl_df()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得在某处读到 as.tibble() as_data_frame()别名 $ c>,但我不知道别名在编程术语中究竟是什么。

I remember reading somewhere that as.tibble() is an alias for as_data_frame(), but I don't know what exactly an alias is in programming terminology. Is it similar to a wrapper?

所以我想我的问题可能归结于 tbl_df() as_data_frame():它们之间有什么区别(如果有的话)?

So I guess my question probably comes down to the difference in possible usages between tbl_df() and as_data_frame(): what are the differences between them, if any?

(非无齿)数据帧 df ,我经常使用以下方法将其变为小齿:

More specifically, given a (non-tibble) data frame df, I often turn it into a tibble by using:

df <- tbl_df(df)

不会

df <- as_data_frame(df)

做同样的事情吗?如果是这样,还有其他情况不能将两个函数 tbl_df() as_data_frame()互换使用得到相同的结果?

do the same thing? If so, are there other cases where the two functions tbl_df() and as_data_frame() can not be used interchangeably to get the same result?

R文档说,


tbl_df()将参数转发给 as_data_frame()

表示 tbl_df() as_data_frame()的包装或别名吗? R文档似乎没有对 as.tibble()讲任何话,但我忘记了我在哪里读到它是 as_data_frame()<的别名。 / code>。另外,显然 as_tibble() as_data_frame()的另一个别名。

does that mean that tbl_df() is a wrapper or alias for as_data_frame()? R documentation doesn't seem to say anything about as.tibble() and I forgot where I read that it was an alias for as_data_frame(). Also, apparently as_tibble() is another alias for as_data_frame().

如果这四个函数确实都是相同的功能,给一个功能四个不同的名称有什么意义?

If these four functions really are all the same function, what is the sense in giving one function four different names? Isn't that more confusing than helpful?

推荐答案

要回答您的是否令人困惑问题,我认为是这样的: )。

To answer your question of "whether it is confusing", I think so :) .

as.tibble as_tibble 相同;两者都只需调用S3方法 as_tibble

as.tibble and as_tibble are the same; both simply call the S3 method as_tibble:

> as.tibble
function (x, ...) 
{
    UseMethod("as_tibble")
}
<environment: namespace:tibble>

as_data_frame tbl_df 不完全相同; tbl_df 调用 as_data_frame

as_data_frame and tbl_df are not exactly the same; tbl_df calls as_data_frame:

> tbl_df
function (data) 
{
    as_data_frame(data)
}
<environment: namespace:dplyr>

注意 tbl_df 在<$ c $中c> dplyr as_data_frame tibble 软件包中:

> as_data_frame
function (x, ...) 
{
    UseMethod("as_data_frame")
}
<environment: namespace:tibble>

但是它当然调用了相同的函数,所以它们是相同的,或者是别名

but of course it calls the same function, so they are "the same", or aliases as you say.

现在,我们来看一下通用方法 as_tibble 之间的区别as_data_frame 。首先,我们看一下每种方法:

Now, we can look at the differences between the generic methods as_tibble and as_data_frame. First, we look at the methods of each:

> 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 定义的其他方法。 code>和 as_tibble.poly 。我不太确定为什么不能同时为 as_data_frame 定义它们。

If you check out the code for as_tibble, you can see that the definitions for many of the as_data_frame methods as well. as_tibble defines two additional methods which aren't defined for as_data_frame, as_tibble.ts and as_tibble.poly. I'm not really sure why they couldn't be also defined for as_data_frame.

as_data_frame 还有两个其他方法,均在 dplyr 中定义: as_data_frame.tbl_cube as_data_frame.grouped_df

as_data_frame has two additional methods, which are both defined in dplyr: as_data_frame.tbl_cube and as_data_frame.grouped_df.

as_data_frame.tbl_cube 使用 as.data.frame 的较弱检查(是的,请忍受),然后调用 as_data_frame

as_data_frame.tbl_cube use the weaker checking of as.data.frame (yes, bear with me) to then call 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 取消对传递的数据帧的分组。

while as_data_frame.grouped_df ungroups the passed dataframe.

总体上,似乎应该将 as_data_frame 视为在 as_tibble之上提供附加功能。 ,除非您要处理 ts poly 对象。

Overall, it seems that as_data_frame should be seen as providing additional functionality over as_tibble, unless you are dealing with ts or poly objects.

这篇关于as.tibble(),as_data_frame()和tbl_df()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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