安全地将data.table重新转换为data.frame [英] safely turn a data.table back into a data.frame

查看:186
本文介绍了安全地将data.table重新转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从对象中移除/移除 data.table 类的最安全的方法是什么,将其转换回data.frame?

What's the safest way to get rid of/remove the data.table class from an object, turning it back into a data.frame?

我问,因为我使用的脚本依赖于下面的代码:

I ask because I'm using script that relies on the following code:

newcol.index <- ncol(my.data) +1
my.data[,newcol.index] <- 3
colnames(my.data)[newcol.index] <- "test"

data.table 像这样,但它使用类 data.frame 的对象工作罚款。

The data.table packages apparently does not like this, but it work fines using objects of class data.frame.

推荐答案

data.tables的 as.data.frame 方法大概是最安全的使用功能。 (尝试键入 getAnywhere(as.data.frame.data.table),以确切了解它的功能。)

The as.data.frame method for data.tables is presumably the safest function to use. (Try typing getAnywhere("as.data.frame.data.table") to see exactly what it does.)

library(data.table)
DT <- data.table(a=1:4, b=letters[c(1,1,2,2)], key="a")

class(as.data.frame(DT))  ## OR:  as(X, "data.frame")
# [1] "data.frame"

这篇关于安全地将data.table重新转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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