[datatable-help] Extract/Subset a data.table with multiple indices
Sterkens, Patrick [JRDBE]
PSTERKEN at its.jnj.com
Tue Jul 10 15:11:09 CEST 2012
R.experts,
The following may be asked & answered but I was not able to find the
answer. So here goes my first R-question.
I am using data.table v1.8.1 to benefit from the := with by=.
(stringAsFactors=F)
Say I have
>DT=data.table(expand.grid(ID=c("ID1","ID2","ID3"),Tx=c("Ctl","Low","Med
ium","High"),>Target=c("IS","G1","G2")))
>setkey(DT, Target,Tx,ID)
>set.seed(1)
>DT[,value:=rnorm(36)]
..and I want to subset the data.table with all "Ctl" (i.e. from the
second key Target) with a binary search.
Doing this
> DT["Ctl"]
Does not generate the desired result, because it searches under the
first key for "Ctl"
Target ID Tx value TX
1: Ctl NA NA NA NA
While vector search does
> DT[Tx=="Ctl"]
ID Tx Target value
1: ID1 Ctl IS 0.6107264
2: ID2 Ctl IS -0.9340976
3: ID3 Ctl IS -1.2536334
4: ID1 Ctl G1 0.5939462
5: ID2 Ctl G1 0.3329504
6: ID3 Ctl G1 1.0630998
7: ID1 Ctl G2 -1.2765922
8: ID2 Ctl G2 -0.5732654
9: ID3 Ctl G2 -1.2246126
Is it possible to do a binary search on one of the keas only ? if yes,
how do you tell data.table that ?
Thanks
Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20120710/74c2e339/attachment.html>
More information about the datatable-help
mailing list