0

我在 data.table 对象中执行一个明显简单的分配。

library(RODBC)
library(data.table)
library(ffbase)
library(plyr)
library(doBy)
library(reshape2)

ST_a = Sys.Date()-7
ST = paste("'",as.character(format(ST_a,"%d.%m.%Y")),"'",sep="")
rd_a = Sys.Date()- 41
rd = paste("'",as.character(format(rd_a,"%d.%m.%Y")),"'",sep="")

ch<-odbcConnect("db",uid="user",pwd="psw")

chunks <- c(seq.Date(from=rd_a, to=ST_a, by = 20),ST_a)

started.at <- proc.time()
Sys.sleep(1)

memory.limit(size=4095)

for (i in 1:(length(chunks)-1)){

start <- paste("'",as.character(format(chunks[i],"%d.%m.%Y")),"'",sep="")
end <- paste("'",as.character(format(chunks[i+1],"%d.%m.%Y")),"'",sep="")

query <- paste("Select * from DATA_LOCAL_V 
        where id='EHPFCMO' and timestamp>",rd," and reference_date>=",start," and reference_date<",end,sep="") 

EHPFC.ck <- data.table(sqlQuery(ch,query,stringsAsFactors=FALSE))

EHPFC.ck[,TIMESTAMP:=TIMESTAMP-60^2]
EHPFC.ck[, pod:=7]
EHPFC.ck[as.POSIXlt(EHPFC.ck$TIMESTAMP)$hour %in% c(20:23), pod:=23]
EHPFC.ck[as.POSIXlt(EHPFC.ck$TIMESTAMP)$hour %in% c(8:19), pod:=19]
EHPFC.ck[,day:=as.Date(trunc(TIMESTAMP))]
EHPFC.ck[,TIMESTAMP:=NULL]
EHPFC.ck <- EHPFC.ck[,mean(VALUE),by=list(ID,REFERENCE_DATE,day,pod)]
setnames(EHPFC.ck,"V1","price")

EHPFC.ck <- as.ffdf(EHPFC.ck)

if (i==1){
EHPFC <- EHPFC.ck
} else {
EHPFC <- ffdfappend(EHPFC,EHPFC.ck[,])
}
rm(EHPFC.ck)
gc()

}

EHPFC <- data.table(EHPFC[,])
EHPFC <- orderBy(~day+pod+REFERENCE_DATE, data=EHPFC)
a <- c(0,log(EHPFC[-1,price]/EHPFC[-nrow(EHPFC),price]))
EHPFC <- EHPFC[,RoR:=a]

通过这样做,我收到以下错误消息:

error in `[.data.table`(EHPFC, , `:=`(RoR, a)) : 
  At an earlier point, this data.table has been copied by R. Avoid key<-, names<- and attr<- which in R currently (and oddly) all copy the whole data.table. Use set* syntax instead to avoid copying: setkey(), setnames() and setattr(). If this message doesn't help, please ask on datatable-help.

对象 EHPFC 如下所示:

EHPFC
           ID REFERENCE_DATE        day pod    price RoR
 [1,] EHPFCMO     2012-10-27 2012-10-27   7 35.34625   0
 [2,] EHPFCMO     2012-10-28 2012-10-27   7 35.34625   0
 [3,] EHPFCMO     2012-10-29 2012-10-27   7 35.34625   0
 [4,] EHPFCMO     2012-10-30 2012-10-27   7 35.34625   0
 [5,] EHPFCMO     2012-10-31 2012-10-27   7 35.34625   0

我有点困惑为什么会这样。

任何想法?提前致谢。

4

0 回答 0