Skip to content

Allow logical specification in .SDcols #1060

Closed

Description

@stefanfritsch

Hi,

these are two connected feature requests for .SDcols:

.1. First, allow logical specifications of columns which would allow stuff like this:

DT[,`:=`(colnames(.SD), lapply(.SD,as.character)), .SDcols=sapply(.SD, is.factor)]

Note that this fails currently because

  • colnames(.SD) in the := specification doesn't work as I mentioned in issue names(.SD) := ... should work #795
  • .SD is not accessible in .SDcols. This would mean that there are two definitions of .SD around (one pre- and one post-.SDcols-selection), but imho the difference is rather intuitive.
  • The resulting logical vector doesn't work.

.2. As a second step it would be really cool if .SDcols would accept selection functions, kind of like Filter:

DT[,(.SDcols):=lapply(.SD,as.character), .SDcols=is.factor]

Other possible use cases apart from type-checks would be basic thresholds and the like.

.3. I smuggled a third wish into the last example. .SDcols already looks like one of the comfort-variables, it would be really nice if we could use it to access the available columns. :)

Thanks for having a look at it even if it doesn't get implemented

Ciao,
Stefan

Activity

  1. arunsrinivasan commented on Mar 5, 2015

    @arunsrinivasan
    Member

    Please file 1 issue per FR/bug, even if they are related. It helps close the issues as and when they are implemented / discussed.

    I'll consider point (1) alone for this issue, and will implement .SDcols taking logical argument. Could you please re-file the other issues when you find time? Thanks.

    The use of colnames(.SD) in the LHS would be a completely different FR (and I think one already exists) as well.

  2. added this to the v1.9.6 milestone on Mar 5, 2015
  3. stefanfritsch commented on Mar 9, 2015

    @stefanfritsch
    Author

    Hi Arun,

    thanks a lot for the patch. 👍

    I'd consider the colnames(.SD) issue part of #795 as mentioned in the report.

    I'll have to think about the functions-as-.SDcols-value thing a bit. I put it in here as an aside because it could be implemented as a quick hack the way you did the logicals (if(is.function) ...) but before I make it its own issue I'd like to find a more flexible and less hacky way; even if it's just a feature request / conversation starter. :)

    Ciao,
    Stefan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions