merge() result incorrect (NAs) when `by` column contains non-ASCII characters

Open
#2,072 5 comments 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
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the supplied merge() example with the reported R 3.3.3 and locale details, then trace the merge() implementation and existing tests for character-key matching. Done means the example returns the expected row for 他 without regressing other merge behavior, with a regression test covering the non-ASCII key.

Written by the indexing model from the issue text.

Description

bug encoding joins platform-specific

When the by column contains non-ASCII characters (e.g, Chinese), the merge() function returns NA unexpectedly.

d1=data.table(a=c("你", "我", "a", "他"), b=1:4)
d2=data.table(a=c("我", "他", "a"), c=3:5)
merge(d1, d2, by="a", all.x=TRUE, sort=FALSE)

should return

   a b  c
1 你 1 NA
2 我 2  3
3  a 3  5
4 他 4  4

right?

but actually turns out to be:

    a b  c
1: 你 1 NA
2: 我 2  3
3:  a 3  5
4: 他 4 NA

Could you look into this problem?

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252       LC_CTYPE=Chinese (Simplified)_People's Republic of China.936
[3] LC_MONETARY=English_United States.1252   LC_NUMERIC=C                                                
[5] LC_TIME=English_United States.1252                          

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

other attached packages:
 [1] data.table_1.10.4  
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.