-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTslots.R
42 lines (34 loc) · 855 Bytes
/
DTslots.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
rm(list=ls())
library(data.table)
# create new class with a single slot that is a data table
objDT <- setClass("objDT", slots=c(x="data.table"))
# assign new variable
test <- objDT(x=as.data.table(sleep))
test@x[,V1:=rnorm(nrow(test@x))]
head(test@x)
# save to a temp file
tf1 <- tempfile()
save(test, file = tf1)
# remove everything except temp file reference
rm(list=setdiff(ls(), "tf1"))
# load and try adding a column to the data table
load(tf1)
test@x[,V2:=rnorm(nrow(test@x))]
# no v2 here
head(test@x)
# try again using DT's fix
rm(list=setdiff(ls(), "tf1"))
load(tf1)
truelength(test@x)
setDT(test@x)
truelength(test@x) == 0
# still no luck
test@x[,V2:=rnorm(nrow(test@x))]
head(test@x)
# this works
test@x$V2 <- rnorm(nrow(test@x))
head(test@x)
# so does this
test@x <- as.data.table(test@x)
test@x[,V3:=rnorm(nrow(test@x))]
head(test@x)