Data Substitution. Table with no head (key (DT), m) using binary search of non-vector scan

If I specify n columns as a key data.table, I know that I can join fewer columns than indicated in this key while I join headfrom key(DT). For example, for n = 2:

X = data.table(A=rep(1:5, each=2), B=rep(1:2, each=5), key=c('A','B'))
X
    A B
 1: 1 1
 2: 1 1
 3: 2 1
 4: 2 1
 5: 3 1
 6: 3 2
 7: 4 2
 8: 4 2
 9: 5 2
10: 5 2

X[J(3)]
   A B
1: 3 1
2: 3 2

There, I just joined the first column of a two-column key DT. I know that I can join both columns of the key as follows:

X[J(3,1)]
   A B
1: 3 1

But how do I subset, using only the second column column of the key column (for example, B==2), but still using binary search and not vector scanning? I know the duplicate:

A subset of data.table on the 2nd column of only the 2nd column key using a binary search of non-vector scan

n. , , , , .

+6
2

, .

 X <- data.table(A=rep(1:5, each=4), B=rep(1:4, each=5), 
                  C = letters[1:20], key=c('A','B','C'))
 make.key <- function(ddd, what){
  # the names of the key columns
  zzz <- key(ddd)
  # the key columns you wish to keep all unique values
  whichUnique <- setdiff(zzz, names(what))
  ## unique data.table (when keyed); .. means "look up one level"
  ud <-  lapply([, ..whichUnique], unique)
  ## append the 'what' columns and  a Cross Join of the new
  ## key columns
  do.call(CJ, c(ud,what)[zzz])
}   

X[make.key(X, what = list(C = c('a','b'))),nomatch=0]
## A B C
## 1: 1 1 a
## 2: 1 1 b

, , .

+5

:

FR # 1007

, , n = 2, ( @mnel ).

+1

All Articles