'Difference between double- precision data type and numeric data type
What is the difference between double-precision data type and numeric data type in R programming?
Solution 1:[1]
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.
Solution 2:[2]
@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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zx8754 |
Solution 2 | user449361 |