Skip to content

character column of x no longer available as ii in x[ y[ii] ] #3669

Closed

Description

@franknarf1

I used code like this for a long time:

# example
library(data.table)
# data.table 1.12.3 IN DEVELOPMENT built 2019-06-19 19:02:40 UTC; travis using 4 threads (see ?getDTthreads).  Latest news: r-datatable.com
iDT = data.table(key = "i_id",
    i_id = c("A", "B", "C", "D"), 
    g = state.name[c(1,1,2, 3)], 
    e_date = as.IDate(c("2019-01-20", "2019-01-20", "2019-01-01", "2019-01-01")),
    e_time = as.ITime(c("14:00", "20:00", "20:00", "20:00"))
)
set.seed(1)
vDT = data.table(i_id = unique(iDT$i_id))[, .(v = runif(5,0,10), p = sample(c(5,5,10,10,10))), by=i_id]

# old code that now errors due to bare i_id in the i= clause
vDT[c("B", "C", "A"), on=.(i_id), .N, by=.EACHI][iDT[i_id, order(e_date, e_time)]]

# longer/clearer version of the same
tmp = vDT[c("B", "C", "A"), on=.(i_id), .N, by=.EACHI]
tmp[iDT[i_id, order(e_date, e_time)]]

but the last line now yields

Error in [.data.table(iDT, i_id, forder(x, e_date, e_time)) :
i_id is not found in calling scope but it is a column of type character. If you wish to select rows where that column contains TRUE, or perhaps that column contains row numbers of itself to select, try DT[(col)], DT[DT$col], or DT[col==TRUE] is particularly clear and is optimized. When the first argument inside DT[...] is a single symbol (e.g. DT[var]), data.table looks for var in calling scope.

Contrary to the error message, i_id is in calling scope from tmp, right? Here's working code illustrating expected behavior:

tmp[iDT[tmp$i_id, order(e_date, e_time)]]
#    i_id N
# 1:    C 5
# 2:    A 5
# 3:    B 5

My code was confusing (took me a minute to figure out what I was trying to do before I could start debugging) so I don't mind if the idiom I used no longer works. I'm just bringing it up in case the scoping is intended to work there.

Activity

  1. added this to the 1.12.4 milestone on Jul 6, 2019
  2. MichaelChirico commented on Sep 2, 2019

    @MichaelChirico
    Member

    The advice of the error message works though right?

    vDT[c("B", "C", "A"), on=.(i_id), .N, by=.EACHI][iDT[(i_id), order(e_date, e_time)]]
    #    i_id  N
    # 1:    A  5
    # 2: <NA> NA
    # 3:    B  5
    # 4:    C  5
    
  3. franknarf1 commented on Sep 2, 2019

    @franknarf1
    ContributorAuthor

    @MichaelChirico Thanks, yes, that works. I was just reporting since it used to work without needing to be wrapped in parentheses. If no one else minds the change in behavior and no rev deps are impacted either, maybe the issue could be closed.

    Side note: I ran into a seg fault when messing around with the example, below. Please let me know if I should open a new issue.

    library(data.table)
    # data.table 1.12.3 IN DEVELOPMENT built 2019-09-02 09:13:56 UTC; root using 4 threads (see ?getDTthreads).  Latest news: r-datatable.com
    iDT = data.table(key = "i_id",
        i_id = c("A", "B", "C", "D"), 
        g = state.name[c(1,1,2, 3)], 
        e_date = as.IDate(c("2019-01-20", "2019-01-20", "2019-01-01", "2019-01-01")),
        e_time = as.ITime(c("14:00", "20:00", "20:00", "20:00"))
    )
    set.seed(1)
    vDT = data.table(i_id = unique(iDT$i_id))[, .(v = runif(5,0,10), p = sample(c(5,5,10,10,10))), by=i_id]
    
    tmp = vDT[c("B", "C", "A"), on=.(i_id), .N, by=.EACHI]
    tmp[iDT[.(.SD$i_id), order(e_date, e_time)]]
    
  4. MichaelChirico commented on Sep 3, 2019

    @MichaelChirico
    Member

    I wouldn't close as it is a regression. If we're going to break something we need to report it.

    That code didn't segfault for me, please report in a new issue if it's still failing from dev master

  5. mattdowle commented on Oct 1, 2019

    @mattdowle
    Member

    @MichaelChirico on #3669 (comment), wrapping with paren like that returns a result by using the character column to join to itself (rather than the variable in calling scope which should work). But then the result is different (the NA is not desired). I'm assuming what @franknarf1 meant by 'it works' was really 'it doesn't error but it means something else and doesn't return what I need'.

    Anyway, looks to be fixed now in dev. Just confirming what fixed it and adding tests...

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions