idxv: Precalculate data.table indices

Description Usage Arguments Value See Also Examples

View source: R/idxv.R

Description

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

Usage

1
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

 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
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

jangorecki/dwtools documentation built on May 18, 2019, 12:24 p.m.