Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shahronak47 opened this issue Nov 14, 2024 · 1 comment

Comments

@shahronak47
Copy link

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
Copy link

joranE commented Nov 14, 2024

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" 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants