将data.table转换为数据帧而不进行复制 [英] Convert a data.table to a data frame without copying

查看:86
本文介绍了将data.table转换为数据帧而不进行复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问答题集询问如何将data.frame转换为data.table而不复制,并演示如何使用手动方法和优秀的 setDT()函数。



如何安全地将 数据表转换回数据框架而不复制?

解决方案

我认为这是一个完全错误的想法,列出所有原因在上面的注释,可能是我不知道,但你可以替换 [。data.table 函数与 [data.frame 。这将防止@Arun提出的关于需要为 [ - functions:

  assignInNamespace([.data.table,getAnywhere(`[.data.frame`),ns =data.table)
/ pre>

通常检查'data.frame'类的所有函数都会因为继承而正常工作。



编辑:Arun的注释提供了一个不太剧烈的方法,因为覆盖核心data.table'[' - 功能本质上是不诚实。我想一个更诚实的方法是unnoad整个NAMESPACE。如果目标是使用 fread ,我想知道是否可以导入该函数和任何依赖。

  setattr(DT,'class','data.frame')


$ b b

但仍然认为这是倒退。


This question and answer set asks how to convert a data.frame to a data.table without copying and shows how to do so using a manual method and the excellent setDT() function.

How would one safely convert back from a data.table to a data frame without copying? The issues and reasoning are similar to the question linked above.

解决方案

I think this is an entirely misguided idea for all the reasons listed in the comments above, and probably ones that I am not aware of, but you can replace the [.data.table function with [data.frame. That would prevent the problem raised by @Arun regarding needing to use an extra argument for the [-function:

 assignInNamespace( "[.data.table", getAnywhere( `[.data.frame`), ns="data.table") 

All of the functions which would normally check for 'data.frame'-class would already have worked normally because of inheritance.

Edit: The comment by Arun provides a much less drastic approach, since over-writing the core data.table '['-function is essentially "dishonest". I suppose a more honest approach would be to unLoad the entire NAMESPACE. If the goal is to just use fread, I wonder if there could just be importation of that functions and any dependencies.

 setattr(DT, 'class', 'data.frame')

But still see this as a "step backward."

这篇关于将data.table转换为数据帧而不进行复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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