[datatable-help] using J() to select for a value that is in something other than the first key

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Jan 19 06:23:51 CET 2012


Hi,

Just to redo the example Matthew is pointing to, I'll put it here.

Note that the current answer is that there is no "neat" way to do it
just, and it takes some intermediary steps for you to get what you
want. Here this amounts to creating the `idx` table w/ the keys
swapped and an `i` column.

There is a feature request to make it neater:
https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1007&group_id=240&atid=978

here's an example:

R> dt <- data.table(a=c('a','a','a','a','b','b','b','b'),
b=c('a','b','a','b','a','b','b','a'),c=1:8,key=c('a','b'))
R> dt
     a b c
[1,] a a 1
[2,] a a 3
[3,] a b 2
[4,] a b 4
[5,] b a 5
[6,] b a 8
[7,] b b 6
[8,] b b 7

R> idx <- dt[,list(b, a, i=1:nrow(dt))]
R> key(idx) <- c('b','a')

Now let's grab all rows from `dt` where the secondary key == 'b'

R> dt[ idx[J("b"), i]$i ]
     a b c
[1,] a b 2
[2,] a b 4
[3,] b b 6
[4,] b b 7

HTH,
-steve

On Wed, Jan 18, 2012 at 11:42 PM, Matthew Dowle <mdowle at mdowle.plus.com> wrote:
> I find Nabble is better for searching the archive. It seems to be
> optimised for discussion list search.  Link on homepage, 'secondary' into
> search box, and e.g. thread " Select from second key but not first".
>
>> Dear Matthew
>>
>> Thank you for trying to guide me. Unfortunately I was not able to really
>> comprehend what I was reading
>>
>> I entered the following into google search "site:
>> lists.r-forge.r-project.org/pipermail/datatable-help/ secondary"
>>
>> I got many hits and I could see a discussion but could not fathom how it
>> related to my question.
>>
>>
>> Would love some help from you or someone else. I want to be able to select
>> by a criterion in the second key and accept all possible values in the
>> first key.
>> Farrel Buchinsky
>> Google Voice Tel: (412) 567-7870
>>
>>
>>
>> On Wed, Jan 18, 2012 at 22:40, Matthew Dowle <mdowle at mdowle.plus.com>
>> wrote:
>>
>>> Hi. Try searching datatable-help for 'secondary'.
>>>
>>> > In setkey it is possible to have multiple columns be a key. There is
>>> > however an order to it. To select all the rows by a particular value
>>> in
>>> > the
>>> > first key is easy
>>> > DT[J("a value from the first key")]
>>> >
>>> > It is even quite easy to select by an AND criteria that incorporates
>>> the
>>> > second key
>>> > DT[J("a value from the first key","a value from the second key")]
>>> >
>>> > But what happens if one wanted to select by only one criteria being a
>>> a
>>> > value that must be present in the second key? I was hoping to get
>>> lucky
>>> by
>>> > just leaving it blank. But it did not see to work.
>>> >
>>> > DT[J(,"a value from the second key")]
>>> >
>>> > I would love to know what you do.
>>> >
>>> >
>>> > Farrel
>>> > _______________________________________________
>>> > 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
>>>
>>>
>>>
>>>
>>>
>>
>
>
>
>
>
>
> _______________________________________________
> 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



-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the datatable-help mailing list