Numeric variable parsed as character when using comma decimals and separator in fread

Open
#5,619 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing the supplied quote1–quote4 examples through data.table::fread, focusing on type inference when the first value is missing, comma decimals are enabled, and separators vary. Trace the fread parsing path and verify that the affected var1 column is read as numeric with the expected missing value and decimal conversion.

Written by the indexing model from the issue text.

Description

fread

Quite new to fread and data.table.
The problem occurred when I had a dataset like quote1, which lead to var1 being interpreted as character, instead of numeric.

Managed to isolate the issue in the following examples. There seem to be some kind of parsing issue when the first value is missing and it is followed by a date. It also seems related to what separator is used.

quote1 <- 'var1,var2\n,2014-06-15\n"15,3",2014-06-16'
data.table::fread(quote1, dec = ',', header = TRUE)

# quote 1 output
> data.table::fread(quote1, dec = ',', header = TRUE)
   var1       var2
1:      2014-06-15
2: 15,3 2014-06-16


quote2 <- 'var1,var2\n"15,3",2014-06-15\n"15,3",2014-06-16'
data.table::fread(quote2, dec = ',', header = TRUE)

# quote 2 output
> data.table::fread(quote2, dec = ',', header = TRUE)
   var1       var2
1: 15.3 2014-06-15
2: 15.3 2014-06-16

quote3 <- 'var1,var2\n,"2014-06-15"\n"15,3","2014-06-16"'
data.table::fread(quote3, dec = ',', header = TRUE)

# quote 3 output
> data.table::fread(quote3, dec = ',', header = TRUE)
   var1       var2
1:   NA 2014-06-15
2: 15.3 2014-06-16

quote4 <- 'var1;var2\n;2014-06-15\n"15,3";2014-06-16'
data.table::fread(quote4, dec = ',', header = TRUE, sep = ';')

# quote 4 output
> data.table::fread(quote4, dec = ',', header = TRUE, sep = ';')
   var1       var2
1:   NA 2014-06-15
2: 15.3 2014-06-16

Trying to use colClasses does not work either:

data.table::fread(quote1, dec = ',', header = TRUE, colClasses = c("var1" = "numeric"))
# output 
   var1       var2
1:      2014-06-15
2: 15,3 2014-06-16
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.