N.Varela
N.Varela

Reputation: 910

R: Why is a not data.table assigned data.frame automatically DT?

I have a data.frame, here an example:

df <- data.frame(object = c("apples","tomatoes", "apples","pears" ),
                 Value = c(50,10,30,40))

I make a copy of this df to keep it in format data.frame:

forDT <- df

And the new copied data.frame I assigned to data.table performing a split of the data:

require(data.table)
tmp = setDT(forDT)[, list(grp=list(.SD)), by=.(object), .SDcols=names(forDT)]
setattr(import_split <- tmp$grp, 'names', paste(tmp$object, sep="."))

In the following step I need the original data.frame which I didn't convert into data.table for further calculations (as I am more familiar with data.frames, thats why I made the copy in the beginning). However, the original df is also of data.table format. What I'm doing wrong? Ideas? Thanks

Upvotes: 0

Views: 82

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

The set* functions in "data.table" do not make copies of your data.

To make a copy, you should use as.data.table or use the copy function from the "data.table" package.

Upvotes: 3

Related Questions