tidyverse / readr

Read flat files (csv, tsv, fwf) into R
https://readr.tidyverse.org
Other
1.01k stars 286 forks source link

Why does "1176413S03" get converted to numeric when using `readr::type_convert` ? #1562

Open shahronak47 opened 1 week ago

shahronak47 commented 1 week ago

Why does these values get converted into numeric when using readr::type_convert ? I would expect them to stay characters.

x <- c("1176413S03", "1176413S06", "1176413S02", "1176413S08", "1176413S05", "1176413S04")
df <- data.frame(x)
str(df)

'data.frame':   6 obs. of  1 variable:
 $ x: chr  "1176413S03" "1176413S06" "1176413S02" "1176413S08" ...

df1 <- readr::type_convert(df)
str(df1)

'data.frame':   6 obs. of  1 variable:
 $ x: num  1.18e+09 1.18e+12 1.18e+08 1.18e+14 1.18e+11 ...
joranE commented 1 week ago

This behavior appears to happen only for the letters D, E, F, L & S (upper or lower case). E makes sense.

res <- purrr::map_chr(
  .x = LETTERS,
  .f = \(x) {
    d <- readr::type_convert(
      df = data.frame(
        x = glue::glue("1176413{letter}03",letter = x)
      )
    )
    class(d$x)
  }
)

setNames(res,LETTERS)

> setNames(res,LETTERS)
          A           B           C           D           E           F 
"character" "character" "character"   "numeric"   "numeric"   "numeric" 
          G           H           I           J           K           L 
"character" "character" "character" "character" "character"   "numeric" 
          M           N           O           P           Q           R 
"character" "character" "character" "character" "character" "character" 
          S           T           U           V           W           X 
  "numeric" "character" "character" "character" "character" "character" 
          Y           Z 
"character" "character"