suppressPackageStartupMessages(library(dwtools))
DT = dw.populate(scenario="fact")
setkeyv(DT,c("cust_code", "prod_code", "geog_code", "time_code", "curr_code"))
print(DT)
Idx = list(
c("cust_code", "prod_code", "geog_code"),
c("cust_code", "geog_code", "curr_code"),
c(2:3)
)
IDX = idxv(DT, Idx)
# list indices
attr(IDX,"DT.key")
# all indices size in MB
cat("Total indices count: ",length(IDX),", size: ",tables(env=as.environment(setNames(IDX,1:length(IDX))),silent=TRUE)[,sum(as.numeric(MB))]," MB\n",sep="")
# Idx[[1]] # filter
DT[CJI(IDX,"id020",847,"AK")]
DT[cust_code=="id020" & prod_code==847 & geog_code=="AK"]
# Idx[[2]] # filter
DT[CJI(IDX,"id006",TRUE,"UT",TRUE,"NOK")]
DT[cust_code=="id006" & geog_code=="UT" & curr_code=="NOK"]
# Idx[[3]] # filter
DT[CJI(IDX,TRUE,323,"OR")]
DT[prod_code==323 & geog_code=="OR"]
# verify DT key
key(DT) # still the same
Run the code above in your browser using DataLab