[datatable-help] Extract/Subset a data.table with multiple indices
Matthew Dowle
mdowle at mdowle.plus.com
Tue Jul 10 15:32:48 CEST 2012
Hi. Welcome to the list.
Short answer is you can't, not yet. However, the buzz phrase to search for
is "secondary key". That should bring up examples and discussions :
i) manual secondary key
ii) rekey using setkey
iii) store a copy with a different key
iv) merge()
Building in secondary keys is FR#1007
https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1007&group_id=240&atid=978
HTH
Matthew
> 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
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
More information about the datatable-help
mailing list