Learn R Programming

dwtools (version 0.8.3.9)

idxv: Precalculate data.table indices

Description

Precalculate indices for data.table, also known as Nth key.

Usage

idxv(DT, Idx, grp = FALSE)

Arguments

DT
data.table for which generate indices.
Idx
list of character vectors, indices to generate.
grp
logical (default FALSE), when TRUE it will include groups id which can be used to speedup aggregations.

Value

list of data.table, including some meta data in attributes.

See Also

CJI

Examples

Run this code
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