Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

safely turn a data.table back into a data.frame

Tags:

r

data.table

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"

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

like image 894
Michael Avatar asked Dec 11 '12 00:12

Michael


1 Answers

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"
like image 100
Josh O'Brien Avatar answered Oct 03 '22 01:10

Josh O'Brien