Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does nchar return the wrong values for large numeric variables in R?

From my understanding the input should be converted to character without specifying

"x = ...character vector, or a vector to be coerced to a character vector"

nchar(2015122514204000000)
# 18 
nchar("2015122514204000000")
# 19 

# Replacing the end zeroes with 111111
nchar(2015122514204111111)
# 19
like image 914
MLEN Avatar asked Jun 04 '26 15:06

MLEN


1 Answers

It is doing the following:

as.character(2015122514204000000)
"2.015122514204e+18"

and

nchar("2.015122514204e+18")

is 18.

Try e.g. options(scipen=999) to prevent scientific notation, and nchar(2015122514204000000) will return 19. Hope this helps!

like image 96
Florian Avatar answered Jun 06 '26 05:06

Florian