UTF encoding in .SDcols causes error when using 'by' argument if there are any accents.

Open
#4,856 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproducible example with data.table 1.13.4 and the UTF-8 column name "á", focusing on the two queries using by and .SDcols. Trace the relevant .SD and grouping handling; done means both queries run without an encoding error and produce the same result as the unencoded example.

Written by the indexing model from the issue text.

Description

encoding

I am working with database that has accentuated characters in the column names and came across an error when trying to use some data.table features. As far as I can tell, the problem lies in that when reading and writing the data I must declare either fwrite(..., bom = T) or fread(..., encoding = "UTF-8"). It is vital to have the correct characters in the column names as well as in each observation and so the data must be imported using UTF-8. Bellow is a reproducible example of my problem. I believe this to be a bug as data.table is able to manage diacritics correctly (as shown in the desired result). It just seems to be misbehaving when using the encoding = "UTF-8" option.

I looked at the dev and found no reference to special characters, or .SDcols handling of diacritics. I also looked at the 1.13.7 milestone page and found nothing relating to this topic either. I tried searching in stack overflow and the queries I came up with did not result in any similar issues.

Thanks.

# Reproducible Example

library(data.table)

set.seed(1)
eg <- data.table("year" = rep(2010:2016, each = 3),
                 "á" = sample(c(1:3, NA), 21, replace = T))

eg2 <- copy(eg)

# Encode variable names in UTF-8
names(eg2) <- enc2utf8(names(eg2))

# These will result in an error
eg2[, lapply(.SD, function(x) uniqueN(x)), by = "year"]
eg2[, lapply(.SD, function(x) uniqueN(x)), by = "year", .SDcols = "á"]

# Desired output
eg[, lapply(.SD, function(x) uniqueN(x)), by = "year"]

# Output of sessionInfo()

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.13.4

loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3 
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.