Repository navigation
Open
Labels
encodingissues related to Encoding
Description
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
```
Metadata
Metadata
Assignees
Labels
encodingissues related to Encoding
Activity
ColeMiller1 commentedon Aug 6, 2020
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 = TRUEoutput on the error as well as the line that does not error that did not have thebystatement. That might give additional hints as to the cause.x-hey commentedon Aug 6, 2020
@ColeMiller1 I have added the
verbose = TRUEoutput. And there is something more about this problem:If I load the data from script, everything works fine
but
However, I don't think this has something to do with
freadnow, because the same problem occurs when I first load data withreadxl::read_excel()on firm's computer, which has a similar environment.ColeMiller1 commentedon Aug 7, 2020
Thanks for the edits and additional info! I can reproduce when I create a
foo.csvfile, 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
freadin addition to the original errors:I will continue to research - thanks again for reporting and providing all of the detail.
x-hey commentedon Aug 11, 2020
Today, I find a work-around——rename the column in English: