13

What is the difference between double-precision data type and numeric data type in R programming?

Bart
  • 473
  • 6
  • 15
Devyani Balyan
  • 175
  • 1
  • 1
  • 10

2 Answers2

15

From stat.ethz.ch:

It is a historical anomaly that R has two names for its floating-point vectors, double and numeric (and formerly had real). double is the name of the type. numeric is the name of the mode and also of the implicit class. As an S4 formal class, use "numeric". The potential confusion is that R has used mode "numeric" to mean ‘double or integer’

We can think of doubles as belonging to numeric. To see this:

 > is.double(1)
 [1] TRUE
 > is.numeric(1)
 [1] TRUE

R normally stores numbers as doubles. Using "numeric()" is the same as "double()." You can also store a number as a single or an integer. Both will be numeric. You may choose to force a number be stored as an integer for performance reasons, but unless you are building a package the trade offs might not be worth your time.

I suggest reading Gillespie's Overview for more info on type and performance.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Peter_Evan
  • 947
  • 10
  • 17
  • 1
    "[Gillespie's Overview](https://csgillespie.github.io/efficientR/)" referenced above gives HTTP 404, probably because the online book was revised. – Morten Engelsmann May 18 '20 at 16:02
1

@tk3: numeric is NOT always identical to double (and real).

> x <- 10
> storage.mode(x)
[1] "double"
> is.numeric(x)
[1] TRUE
> x <- as.integer(x)
> storage.mode(x)
[1] "integer"
> is.numeric(x)
[1] TRUE
user449361
  • 63
  • 4