[datatable-help] `with=F` in the `i` Argument

Michael Smith my.r.help at gmail.com
Sat Jun 21 05:19:00 CEST 2014


Hi Arun,

If `is.object` gives `FALSE` and you just have a list, you could wrap it
in `unlist` as follows. It gives the same result for your cases. (These
are just my two cents, maybe someone else has a different opinion.)

require(data.table)
DT <- data.table(x=c(1,4,3,2), y=c(8,6,5,7), z=c(10,12,11,9))

## Case A.
DT[base::order(DT[, "x", with=FALSE])]  # OK.
## Case B.
DT[base::order(list(x))]                # Not OK.
DT[base::order(unlist(list(x)))]        # Same as case A.

# Case C.
DT[base::order(DT[, "x", with=FALSE], DT[, "y", with=FALSE])] # OK.
# Case D.
DT[base::order(list(x), list(y))]         # Not OK.
DT[base::order(unlist(list(x), list(y)))] # Same as case C.

## Case E.
DT[base::order(DT[, c("x", "y"), with=FALSE])] # Pads NA for `y`.
## Case F.
DT[base::order(list(x, y))]             # Not OK.
DT[base::order(unlist(list(x, y)))]     # Same as case E.


Thanks,
M




On 06/21/2014 08:25 AM, Arunkumar Srinivasan wrote:
> Michael,
> 
> Note that in your case, you can also do:
> 
> |DT <- data.table(a = 1:4, b = 8:5)
> for (i in c("a", "b"))
>     DT[order(DT[[i]])]
> |
> 
> At the moment, I’m more inclined towards giving an error when any of the
> arguments to |order(.)| results in a |list|. The message could be
> something like:
> 
> |DT[order(.)] on data.tables is optimised internally to use data.table's fast ordering. Since the behaviour of base:::order seems inconsistent in the way it handles list input - for ex: compare DT[order(list(x))] and DT[order(data.table(x))], we do not support list columns as input here. If you're sure, you can use `DT[base:::order(.)]` explicitly. However, this can be avoided most of the times by using `[[` to access specified columns to result in a vector.
> |
> 
> What do you (all) think?
> 
> 
> Arun
> 
> From: Arunkumar Srinivasan aragorn168b at gmail.com
> <mailto:aragorn168b at gmail.com>
> Reply: Arunkumar Srinivasan aragorn168b at gmail.com
> <mailto:aragorn168b at gmail.com>
> Date: June 20, 2014 at 11:47:22 PM
> To: Michael Smith my.r.help at gmail.com <mailto:my.r.help at gmail.com>
> Cc: datatable-help at lists.r-forge.r-project.org
> datatable-help at lists.r-forge.r-project.org
> <mailto:datatable-help at lists.r-forge.r-project.org>
> Subject: Re: [datatable-help] `with=F` in the `i` Argument
> 
>> This is a really tricky one. I was just trying to fix it when I
>> recollected the issues with |base:::order| from the time during
>> implementation.
>>
>> Consider this case:
>>
>> |require(data.table)
>> DT <- data.table(x=c(1,4,3,2), y=c(8,6,5,7), z=c(10,12,11,9))
>> |
>>
>> Consider the cases A and B below:
>>
>> |# case A
>> DT[base:::order(DT[, "x", with=FALSE])]
>> #    x y  z
>> # 1: 1 8 10
>> # 2: 2 7  9
>> # 3: 3 5 11
>> # 4: 4 6 12
>> |
>>
>> Intended right result. Great!
>>
>>
>>       B:
>>
>> |# case B
>> DT[base:::order(list(x))]
>> #    x y  z
>> # 1: 1 8 10
>> |
>>
>> What just happened?!? So, basically if the list gives |TRUE| for
>> |is.object(.)|, it understands what the opeation is, correctly. But if
>> it’s /just/ a list, no idea how to deal with it. Also it silently
>> returns undesirable result (imo).
>>
>> Similar to the above cases, compare these two:
>>
>> |# case C
>> DT[base:::order(DT[, "x", with=FALSE], DT[, "y", with=FALSE])]
>> # vs
>> # case D
>> DT[base:::order(list(x), list(y))]
>> |
>>
>>
>>       Even more crazy case:
>>
>> |# case E
>> DT[base:::order(DT[, c("x", "y"), with=FALSE])]
>> # vs
>> # case F
>> DT[base:::order(list(x,y))]
>> |
>>
>> While we were testing and implementing |forder|, obviously it dint
>> occur to check with the argument to |order(.)| with a |data.table|.
>> And in spite of the fact that the output for |DT[order(list(x))]| is a
>> bit strange and even dangerous, to be consistent with |base:::order|,
>> we had implemented it the same way.
>>
>> Now, I’m not so sure.. Any ideas justifying these differences?
>>
>>
>>
>> Arun
>>
>> From: Michael Smith my.r.help at gmail.com <mailto:my.r.help at gmail.com>
>> Reply: Michael Smith my.r.help at gmail.com <mailto:my.r.help at gmail.com>
>> Date: June 15, 2014 at 5:02:46 AM
>> To: G See gsee000 at gmail.com <mailto:gsee000 at gmail.com>
>> Cc: datatable-help at lists.r-forge.r-project.org
>> datatable-help at lists.r-forge.r-project.org
>> <mailto:datatable-help at lists.r-forge.r-project.org>
>> Subject:  Re: [datatable-help] `with=F` in the `i` Argument
>>
>>> Devs,
>>>
>>> Is this a bug? It works in 1.9.2 but not in the 1.9.3 development
>>> version:
>>>
>>> DT <- data.table(a = 1:4, b = 8:5)
>>> for (i in c("a", "b"))
>>> print(DT[order(DT[, i, with = FALSE])])
>>>
>>> Error in forder(DT, DT[, i, with = FALSE]) :
>>> Column '1' is type 'list' which is not supported for ordering currently.
>>>
>>>
>>> Thanks,
>>>
>>> M
>>>
>>>
>>> On 05/31/2014 12:44 PM, G See wrote:
>>> > Hi Michael,
>>> >
>>> > I would use get()
>>> >
>>> > DT <- data.table(a = 1:4, b = 8:5)
>>> > for (i in c("a", "b"))
>>> > print(DT[order(get(i))])
>>> >
>>> > For what it's worth, your solution doesn't seem to work in data.table
>>> > 1.9.3 (svn rev. 1278):
>>> >
>>> >> for (i in c("a", "b"))
>>> > + print(DT[order(DT[, i, with = FALSE])])
>>> > Error in forder(DT, DT[, i, with = FALSE]) :
>>> > Column '1' is type 'list' which is not supported for ordering currently.
>>> >
>>> >
>>> > HTH,
>>> > Garrett
>>> >
>>> > On Fri, May 30, 2014 at 11:01 PM, Michael Smith <my.r.help at gmail.com> wrote:
>>> >> All,
>>> >>
>>> >> I'm trying to order the rows according to several columns at a time:
>>> >>
>>> >> DT <- data.table(a = 1:4, b = 8:5)
>>> >> for (i in c("a", "b"))
>>> >> print(DT[order(i), with = FALSE])
>>> >>
>>> >> It doesn't work, since `with` seems to be about the `j` argument, but
>>> >> not the `i` argument, according to `?data.table`.
>>> >>
>>> >> I found the following workaround, but wonder whether there is a more
>>> >> elegant way to do it:
>>> >>
>>> >> for (i in c("a", "b"))
>>> >> print(DT[order(DT[, i, with = FALSE])])
>>> >>
>>> >> Thanks,
>>> >> M
>>> >> _______________________________________________
>>> >> 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
> 


More information about the datatable-help mailing list