0

There are certain numbers that when read with fread() don't match, like 2022.33. Other options like read.csv or read.table work fine. It seems that the fread() isn't correct here, is there a way to have it match read.table?

Reproducible example:

library(data.table)
# Version 1.14.8

write.csv('2022.33', file = './test.csv', row.names = FALSE)

fread_results <- fread('./test.csv')
base_results <- read.csv('./test.csv')

fread_results$x - base_results$x
# Returns 2.273737e-13
identical(fread_results$x, base_results$x)
# False
badmax
  • 645
  • 2
  • 5
  • 12
  • 1
    Not sure what you're seeing, but `fread("text.csv")` for me returned `structure(list(x = 2022.33), row.names = c(NA, -1L), class = c("data.table", "data.frame"))`, which seems fine to me. And `identical(fread_results$x - base_results$x, 0)` is true. – r2evans Jun 29 '23 at 18:33
  • 1
    Representation of floating point numbers is not exact in general. Lots of discussion, examples, and workarounds at the linked FAQ. – Gregor Thomas Jun 29 '23 at 18:35
  • FYI, "unable to read" suggests an error or warning of some sort, which does not appear to be the case here. If you look at `fread_results`, it has a column named `x` (expected), it is class `numeric`, and its single row has a value of `2022.33`, none of that is an error. – r2evans Jun 29 '23 at 18:36
  • @r2evans `identical(fread_results$x, base_results$x)` is false. The source is the same so reading the number in a different way leads to different, undocumented results without warning. This leads to downstream failures when for example trying to merge on the number and what function was used to read it becomes a factor. – badmax Jun 29 '23 at 20:37
  • 1
    This is not an `fread` problem or an R problem but a problem common to computing. Testing for exact equality on floating point numbers for merges or other purposes is a Bad Idea. See the linked FAQ for explanation and work-arounds. `identical()` may return `FALSE`, `all.equal` should return `TRUE`. For a more general treatment, see this [language-agnostic version of the FAQ](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – Gregor Thomas Jun 29 '23 at 20:47

0 Answers0