-
Notifications
You must be signed in to change notification settings - Fork 0
/
hourPrep.R
51 lines (43 loc) · 2.66 KB
/
hourPrep.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
43
44
45
46
47
48
49
50
51
# class hourPrep
hourPrep = setRefClass("hourPrep",
fields =
list(
filePath="character",
libraryList ="vector"
),
methods=
list(
initialize = function(){
.self$filePath="~/Desktop/Delete/rstudio-export/all_hist_data_60minute.rds"
.self$libraryList=c("dplyr","data.table","fts","zoo")
.self$loadLibrary()
},
loadLibrary = function(){
lapply(.self$libraryList, function(x) eval(parse(text=paste("library(",x,")") ) ) )
},
loadData = function(){
path=.self$filePath
readRDS(path) %>% as.data.table()
},
lagData = function( DT,sortCols,sortDirection, cols, prefix="P", moveN=1){
DT=DT %>% as.data.table()
setorderv(DT, sortCols, sortDirection)
anscols=paste0(prefix,moveN,cols)
DT[,(anscols) := shift(.SD, moveN, NA,"lag"), .SDcols=cols]
},
leadData = function( DT,sortCols,sortDirection, cols, prefix="N", moveN=1){
DT=DT %>% as.data.table()
setorderv(DT, sortCols, sortDirection)
anscols=paste0(prefix,moveN,cols)
DT[,(anscols) := shift(.SD, moveN, NA,"lead"), .SDcols=cols]
},
dayNumAttached = function(DT, sortCols, sortDirection, dateCol="DATE"){
DT =DT %>% as.data.table()
setorderv(DT, sortCols, sortDirection)
dateselect=paste0("daynumdt = data.table(dateseries=DT$",dateCol," %>% sort(), stringsAsFactors=FALSE)")
dataframebuild = paste0("daynumdt$DAYNUM = as.numeric(row.names(daynumdt))")
dayoplist = c(dateselect, dataframebuild)
lapply(dayoplist, function(x) parse(eval(text=x)))
}
)#method ends
)#class ends