What's the difference between integer class and numeric class in R
Asked Answered
S

5

121

I want to preface this by saying I'm an absolute programming beginner, so please excuse how basic this question is.

I'm trying to get a better understanding of "atomic" classes in R and maybe this goes for classes in programming in general. I understand the difference between a character, logical, and complex data classes, but I'm struggling to find the fundamental difference between a numeric class and an integer class.

Let's say I have a simple vector x <- c(4, 5, 6, 6) of integers, it would make sense for this to be an integer class. But when I do class(x) I get [1] "numeric". Then if I convert this vector to an integer class x <- as.integer(x). It return the same exact list of numbers except the class is different.

My question is why is this the case, and why the default class for a set of integers is a numeric class, and what are the advantages and or disadvantages of having an integer set as numeric instead of integer.

Somali answered 14/5, 2014 at 16:21 Comment(4)
Does as.integer(c(4.1, 5.2, 6.3, 6.4)) help you understanding the difference? You need to understand that the internal representation and what is printed are not the same at all. Anyway, do some reading about data types in computer languages.Focal
In the "Related" column to the right is this question: #8805279Swick
Try x <- 1; is.integer(x); is.numeric(x), then x <- 1L; is.integer(x); is.numeric(x) and you may be able to see a little of the difference. Integer classes are used more to pass variables from C constructs and also in R structures. Though, there's a lot more to this.Madrigalist
@Focal I didn't think about classes specifying precision. I'm used to using float methods in order to manipulate integer classes. It makes sense.Somali
M
108

There are multiple classes that are grouped together as "numeric" classes, the 2 most common of which are double (for double precision floating point numbers) and integer. R will automatically convert between the numeric classes when needed, so for the most part it does not matter to the casual user whether the number 3 is currently stored as an integer or as a double. Most math is done using double precision, so that is often the default storage.

Sometimes you may want to specifically store a vector as integers if you know that they will never be converted to doubles (used as ID values or indexing) since integers require less storage space. But if they are going to be used in any math that will convert them to double, then it will probably be quickest to just store them as doubles to begin with.

Mebane answered 14/5, 2014 at 17:7 Comment(0)
A
59

Patrick Burns on Quora says:

First off, it is perfectly feasible to use R successfully for years and not need to know the answer to this question. R handles the differences between the (usual) numerics and integers for you in the background.

> is.numeric(1)

[1] TRUE

> is.integer(1)

[1] FALSE

> is.numeric(1L)

[1] TRUE

> is.integer(1L)

[1] TRUE

(Putting capital 'L' after an integer forces it to be stored as an integer.)

As you can see "integer" is a subset of "numeric".

> .Machine$integer.max

[1] 2147483647

> .Machine$double.xmax

[1] 1.797693e+308

Integers only go to a little more than 2 billion, while the other numerics can be much bigger. They can be bigger because they are stored as double precision floating point numbers. This means that the number is stored in two pieces: the exponent (like 308 above, except in base 2 rather than base 10), and the "significand" (like 1.797693 above).

Note that 'is.integer' is not a test of whether you have a whole number, but a test of how the data are stored.

One thing to watch out for is that the colon operator, :, will return integers if the start and end points are whole numbers. For example, 1:5 creates an integer vector of numbers from 1 to 5. You don't need to append the letter L.

> class(1:5)
[1] "integer"

Reference: https://www.quora.com/What-is-the-difference-between-numeric-and-integer-in-R

Arawakan answered 4/9, 2015 at 15:32 Comment(5)
Taken exactly from Quora post Right? You could have just mentioned the reference!Haversack
quora.com/…Haversack
The linked URL does it right at the top - both store "numbers," integer class are stored as whole numbers, numeric class can have decimals. Thank you Rana for posting the URL. I have been wondering this myself for some time.Adriel
@SrujanBarai, In general, folks get flagged if they just post a link to some other place. It's considered useful to repeat the answer or the essence thereof. What if the information linked to goes away?Mablemabry
@Mablemabry yes but along with the post, you should also post the reference. This post initially didn't have the ref to the original post.Haversack
W
8

To quote the help page (try ?integer), bolded portion mine:

Integer vectors exist so that data can be passed to C or Fortran code which expects them, and so that (small) integer data can be represented exactly and compactly.

Note that current implementations of R use 32-bit integers for integer vectors, so the range of representable integers is restricted to about +/-2*10^9: doubles can hold much larger integers exactly.

Like the help page says, R's integers are signed 32-bit numbers so can hold between -2147483648 and +2147483647 and take up 4 bytes.

R's numeric is identical to an 64-bit double conforming to the IEEE 754 standard. R has no single precision data type. (source: help pages of numeric and double). A double can store all integers between -2^53 and 2^53 exactly without losing precision.

We can see the data type sizes, including the overhead of a vector (source):

> object.size(1:1000)
4040 bytes
> object.size(as.numeric(1:1000))
8040 bytes
Whirl answered 1/8, 2019 at 1:59 Comment(0)
D
1

To my understanding - we do not declare a variable with a data type so by default R has set any number without L to be a numeric. If you wrote:

> x <- c(4L, 5L, 6L, 6L)
> class(x)
>"integer" #it would be correct

Example of Integer:

> x<- 2L
> print(x)

Example of Numeric (kind of like double/float from other programming languages)

> x<-3.4
> print(x)
Distressed answered 9/10, 2018 at 14:24 Comment(1)
watch out a range like 1:5 will create integers.Whirl
S
-2

Numeric is an umbrella term for several types of classes (e.g. double and integer). Integers are numbers which do not have decimal points and thus are stored with minimal space in memory. Use the integer class only when doing computations with such numbers, otherwise revert to numeric.

Shuman answered 8/9, 2022 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.