16

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.

Michael
  • 5,808
  • 4
  • 30
  • 39
  • 1
    this doesn't even work on a data.frame. `DF <- data.frame(a=1:2,b=1:2); colnames(DF)[3] <- 'z'` gives the error *Error in `colnames<-`(`*tmp*`, value = c("a", "b", "hello")) : 'names' attribute [3] must be the same length as the vector [2]* – mnel Dec 11 '12 at 00:18
  • Edited the post to describe exactly what wasn't working in `data.table` – Michael Dec 11 '12 at 00:24

3 Answers3

17

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"
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
4

If you are willing to convert your script to data.table, you can use use := to assign by reference, this will automatically assign to the (ncol(youdata)+1)th column, and you can pass a character vector of the names to the LHS of the function. It will assign by reference, so no copying!

DT <- data.table(a = 1, b = 2)

DT[,'test' := 3]


DT
   a b test
1: 1 2    3
mnel
  • 113,303
  • 27
  • 265
  • 254
2

This is an example of how to convert from data.table to data frame

library(tidyverse)
library(data.table)

df <- data.frame(a = 1:5, b = 6:10, c = LETTERS[5:9])
class(df)
#[1] "data.frame"

df <- data.table(df)
class(df)
#[1] "data.table" "data.frame"

class(df) <- class(as.data.frame(df))
class(df)
#[1] "data.frame"
Tho Vu
  • 1,304
  • 2
  • 8
  • 20