Skip to content

object not found after adding by #4665

Open

Description

@x-hey

Recently, when wrangling data in data.table, I get an error below after adding by :

Error in `[.data.table`(DT, , <some j expression>, : object '<some variable>' not found

# Minimal reproducible example

DT = fread("foo.csv", encoding = "UTF-8")
DT

##     活动类别 持续时间           开始时间           结束时间       备注   Tags
##  1:   活动一 00:08:00 2019-11-0719:39:00 2019-11-0719:47:00 故人具鸡黍       
##  2:   活动二 00:44:00 2019-08-2408:00:00 2019-08-2408:45:00 邀我至田家       
##  3:   活动三 00:41:00 2019-07-2911:26:00 2019-07-2912:07:00 绿树村边合       
##  4:   活动四 00:37:00 2019-12-2816:49:00 2019-12-2817:27:00 青山郭外斜 孟浩然
##  5:   活动三 01:15:00 2019-12-2613:45:00 2019-12-2615:00:00                  
##  6:   活动五 00:20:00 2019-10-3107:36:00 2019-10-3107:57:00 开轩面场圃       
##  7:   活动六 00:31:00 2019-07-2018:24:00 2019-07-2018:55:00 把酒话桑麻   李白
##  8:   活动四 00:31:00 2019-09-0109:09:00 2019-09-0109:41:00 待到重阳日       
##  9:   活动七 01:23:00 2019-09-2121:06:00 2019-09-2122:30:00              杜甫
## 10:   活动三 00:54:00 2019-07-1018:09:00 2019-07-1019:04:00 还来就菊花

some data operations

DT[, `:=`(持续时间 = as.ITime(持续时间),
          开始时间 = as.POSIXct(开始时间, format = "%Y-%m-%d %H:%M"),
          结束时间 = as.POSIXct(结束时间, format = "%Y-%m-%d %H:%M"))
   ][, 年月 := format(开始时间, "%Y年%m月")]

this works

DT[, sum(持续时间 < as.ITime("04:00:00"))]

## Detected that j uses these columns: 持续时间 
## [1] 10

but this fails

DT[, sum(持续时间 < as.ITime("04:00:00")), by = "年月"]

## Detected that j uses these columns: 持续时间 
## Finding groups using forderv ... forder.c received 10 rows and 1 columns
## 0.000s elapsed (0.000s cpu) 
## Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu) 
## Getting back original order ... forder.c received a vector type 'integer' length 6
## 0.000s elapsed (0.000s cpu) 
## lapply optimization is on, j unchanged as 'sum(持续时间 < as.ITime("04:00:00"))'
## GForce is on, left j unchanged
## Old mean optimization is on, left j unchanged.
## Making each group and running j (GForce FALSE) ... Error in `[.data.table`(DT, , sum(持续时间 < as.ITime("04:00:00")), by = "年月",  : 
##                                                                              object '持续时间' not found

# Text in foo.csv

活动类别,持续时间,开始时间,结束时间,备注,Tags
活动一,00:08:00,2019-11-0719:39:00,2019-11-0719:47:00,故人具鸡黍,
活动二,00:44:00,2019-08-2408:00:00,2019-08-2408:45:00,邀我至田家,
活动三,00:41:00,2019-07-2911:26:00,2019-07-2912:07:00,绿树村边合,
活动四,00:37:00,2019-12-2816:49:00,2019-12-2817:27:00,青山郭外斜,孟浩然
活动三,01:15:00,2019-12-2613:45:00,2019-12-2615:00:00,,
活动五,00:20:00,2019-10-3107:36:00,2019-10-3107:57:00,开轩面场圃,
活动六,00:31:00,2019-07-2018:24:00,2019-07-2018:55:00,把酒话桑麻,李白
活动四,00:31:00,2019-09-0109:09:00,2019-09-0109:41:00,待到重阳日,
活动七,01:23:00,2019-09-2121:06:00,2019-09-2122:30:00,,杜甫
活动三,00:54:00,2019-07-1018:09:00,2019-07-1019:04:00,还来就菊花,

# Related SO question

Inconsistent behavior of data.table package (or its fread() function) with Chinese characters

# Output of sessionInfo()

sessionInfo()

## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] data.table_1.13.0
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.6.0  magrittr_1.5    tools_3.6.0     htmltools_0.4.0
##  [5] yaml_2.2.1      Rcpp_1.0.3      stringi_1.4.6   rmarkdown_2.1  
##  [9] knitr_1.28      stringr_1.4.0   xfun_0.12       digest_0.6.25  
## [13] rlang_0.4.6     evaluate_0.14
```

Activity

  1. ColeMiller1 commented on Aug 6, 2020

    @ColeMiller1
    Contributor

    Thank you for reporting.

    I was unable to reproduce, but it could be a locale issue for me. For what it's worth, I tried to change it into a (mostly) English reprex but did not see a similar error. Below that is my attempt at reproducing - I have a lot of encoding errors.

    Note it's always helpful to have something that is easy to copy and paste. Related, it may help if you edit your original post to include verbose = TRUE output on the error as well as the line that does not error that did not have the by statement. That might give additional hints as to the cause.

    library(data.table)
    DT = fread(
      "a,b,c
    00:08:00,2019-11-0719:39:00,2019-11-0719:47:00
    00:44:00,2019-08-2408:00:00,2019-08-2408:45:00
    00:41:00,2019-07-2911:26:00,2019-07-2912:07:00
    00:37:00,2019-12-2816:49:00,2019-12-2817:27:00
    01:15:00,2019-12-2613:45:00,2019-12-2615:00:00
    00:20:00,2019-10-3107:36:00,2019-10-3107:57:00
    00:31:00,2019-07-2018:24:00,2019-07-2018:55:00
    00:31:00,2019-09-0109:09:00,2019-09-0109:41:00
    01:23:00,2019-09-2121:06:00,2019-09-2122:30:00
    00:54:00,2019-07-1018:09:00,2019-07-1019:04:00", encoding = "UTF-8"
    )
    
    DT[, `:=`(a = as.ITime(a),
              b = as.POSIXct(b, format = "%Y-%m-%d %H:%M"),
              c = as.POSIXct(c, format = "%Y-%m-%d %H:%M"))
    ][, by_grp := format(b, "%Y?%m?")]
    DT
    #>            a                   b                   c   by_grp
    #>      <ITime>              <POSc>              <POSc>   <char>
    #>  1: 00:08:00 2019-11-07 19:39:00 2019-11-07 19:47:00 2019?11?
    #>  2: 00:44:00 2019-08-24 08:00:00 2019-08-24 08:45:00 2019?08?
    #>  3: 00:41:00 2019-07-29 11:26:00 2019-07-29 12:07:00 2019?07?
    #>  4: 00:37:00 2019-12-28 16:49:00 2019-12-28 17:27:00 2019?12?
    #>  5: 01:15:00 2019-12-26 13:45:00 2019-12-26 15:00:00 2019?12?
    #>  6: 00:20:00 2019-10-31 07:36:00 2019-10-31 07:57:00 2019?10?
    #>  7: 00:31:00 2019-07-20 18:24:00 2019-07-20 18:55:00 2019?07?
    #>  8: 00:31:00 2019-09-01 09:09:00 2019-09-01 09:41:00 2019?09?
    #>  9: 01:23:00 2019-09-21 21:06:00 2019-09-21 22:30:00 2019?09?
    #> 10: 00:54:00 2019-07-10 18:09:00 2019-07-10 19:04:00 2019?07?
    
    DT[, sum(a < as.ITime("04:00:00"))]
    #> [1] 10
    DT[, sum(a < as.ITime("04:00:00")), by = "by_grp"]
    #>      by_grp    V1
    #>      <char> <int>
    #> 1: 2019?11?     1
    #> 2: 2019?08?     1
    #> 3: 2019?07?     3
    #> 4: 2019?12?     2
    #> 5: 2019?10?     1
    #> 6: 2019?09?     2
    library(data.table)
    DT = fread("活动类别,持续时间,开始时间,结束时间,备注,Tags
    活动一,00:08:00,2019-11-0719:39:00,2019-11-0719:47:00,故人具鸡黍,
    活动二,00:44:00,2019-08-2408:00:00,2019-08-2408:45:00,邀我至田家,
    活动三,00:41:00,2019-07-2911:26:00,2019-07-2912:07:00,绿树村边合,
    活动四,00:37:00,2019-12-2816:49:00,2019-12-2817:27:00,青山郭外斜,孟浩然
    活动三,01:15:00,2019-12-2613:45:00,2019-12-2615:00:00,,
    活动五,00:20:00,2019-10-3107:36:00,2019-10-3107:57:00,开轩面场圃,
    活动六,00:31:00,2019-07-2018:24:00,2019-07-2018:55:00,把酒话桑麻,李白
    活动四,00:31:00,2019-09-0109:09:00,2019-09-0109:41:00,待到重阳日,
    活动七,01:23:00,2019-09-2121:06:00,2019-09-2122:30:00,,杜甫
    活动三,00:54:00,2019-07-1018:09:00,2019-07-1019:04:00,还来就菊花,", encoding = "UTF-8")
    DT
    DT[, `:=`(持续时间 = as.ITime(持续时间),
                  开始时间 = as.POSIXct(开始时间, format = "%Y-%m-%d %H:%M"),
                  结束时间 = as.POSIXct(结束时间, format = "%Y-%m-%d %H:%M"))
    ][, 年月 := format(开始时间, "%Y年%m月")]
    
    DT[, sum(持续时间 < as.ITime("04:00:00"))]
    DT[, sum(持续时间 < as.ITime("04:00:00")), by = "年月"]
  2. x-hey commented on Aug 6, 2020

    @x-hey
    Author

    @ColeMiller1 I have added the verbose = TRUE output. And there is something more about this problem:

    If I load the data from script, everything works fine

    DT2 = fread("
    活动类别,持续时间,开始时间,结束时间,备注,Tags
    活动一,00:08:00,2019-11-0719:39:00,2019-11-0719:47:00,故人具鸡黍,
    活动二,00:44:00,2019-08-2408:00:00,2019-08-2408:45:00,邀我至田家,
    活动三,00:41:00,2019-07-2911:26:00,2019-07-2912:07:00,绿树村边合,
    活动四,00:37:00,2019-12-2816:49:00,2019-12-2817:27:00,青山郭外斜,孟浩然
    活动三,01:15:00,2019-12-2613:45:00,2019-12-2615:00:00,,
    活动五,00:20:00,2019-10-3107:36:00,2019-10-3107:57:00,开轩面场圃,
    活动六,00:31:00,2019-07-2018:24:00,2019-07-2018:55:00,把酒话桑麻,李白
    活动四,00:31:00,2019-09-0109:09:00,2019-09-0109:41:00,待到重阳日,
    活动七,01:23:00,2019-09-2121:06:00,2019-09-2122:30:00,,杜甫
    活动三,00:54:00,2019-07-1018:09:00,2019-07-1019:04:00,还来就菊花,
    ")
    
    DT2[, `:=`(持续时间 = as.ITime(持续时间),
              开始时间 = as.POSIXct(开始时间, format = "%Y-%m-%d %H:%M"),
              结束时间 = as.POSIXct(结束时间, format = "%Y-%m-%d %H:%M"))
       ][, 年月 := format(开始时间, "%Y年%m月")]
    
    ## Detected that j uses these columns: 持续时间 
    ## Finding groups using forderv ... forder.c received 10 rows and 1 columns
    ## 0.000s elapsed (0.000s cpu) 
    ## Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu) 
    ## Getting back original order ... forder.c received a vector type 'integer' length 6
    ## 0.000s elapsed (0.000s cpu) 
    ## lapply optimization is on, j unchanged as 'sum(持续时间 < as.ITime("04:00:00"))'
    ## GForce is on, left j unchanged
    ## Old mean optimization is on, left j unchanged.
    ## Making each group and running j (GForce FALSE) ... 
    ##   collecting discontiguous groups took 0.000s for 6 groups
    ##   eval(j) took 0.002s for 6 calls
    ## 0.000s elapsed (0.000s cpu)
    
    ##          年月 V1
    ## 1: 2019年11月  1
    ## 2: 2019年08月  1
    ## 3: 2019年07月  3
    ## 4: 2019年12月  2
    ## 5: 2019年10月  1
    ## 6: 2019年09月  2
    
    DT2[, sum(持续时间 < as.ITime("04:00:00")), verbose = TRUE]
    
    ## Detected that j uses these columns: 持续时间
    
    ## [1] 10
    

    but

    identical(DT, DT2)
    
    ## [1] TRUE
    

    However, I don't think this has something to do with fread now, because the same problem occurs when I first load data with readxl::read_excel() on firm's computer, which has a similar environment.

  3. ColeMiller1 commented on Aug 7, 2020

    @ColeMiller1
    Contributor

    Thanks for the edits and additional info! I can reproduce when I create a foo.csv file, set my locale to Chinese, and read it into R. I am still investigating but it seems like accounting for the column during the grouping operations.

    One other interesting note is that with these characters we cannot activate GForce when it's read in with fread in addition to the original errors:

    DT[, sum(持续时间), by = "年月", verbose = T]
    ##GForce is on, left j unchanged
    
    DT[, a := 1:10]
    DT[, sum(a), by = "年月", verbose = T]
    ##GForce optimized j to 'gsum(a)'

    I will continue to research - thanks again for reporting and providing all of the detail.

  4. x-hey commented on Aug 11, 2020

    @x-hey
    Author

    Today, I find a work-around——rename the column in English:

    setnames(DT, old = "持续时间", new = "duration")
    
    DT[, sum(duration < as.ITime("04:00:00")), by = "年月", verbose = TRUE]
    
    ## Detected that j uses these columns: duration 
    ## Finding groups using forderv ... forder.c received 10 rows and 1 columns
    ## 0.000s elapsed (0.000s cpu) 
    ## Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu) 
    ## Getting back original order ... forder.c received a vector type 'integer' length 6
    ## 0.000s elapsed (0.000s cpu) 
    ## lapply optimization is on, j unchanged as 'sum(duration < as.ITime("04:00:00"))'
    ## GForce is on, left j unchanged
    ## Old mean optimization is on, left j unchanged.
    ## Making each group and running j (GForce FALSE) ... 
    ##   collecting discontiguous groups took 0.000s for 6 groups
    ##   eval(j) took 0.001s for 6 calls
    ## 0.000s elapsed (0.000s cpu)
    
    ##          年月 V1
    ## 1: 2019年11月  1
    ## 2: 2019年08月  1
    ## 3: 2019年07月  3
    ## 4: 2019年12月  2
    ## 5: 2019年10月  1
    ## 6: 2019年09月  2
    
  5. added
    encoding
    on Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    encoding

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions