What is the difference between Numeric and integer vectors?

Asked

Viewed 1,124 times

5

In R, identical vectors (containing the same numbers) can assume the classes numeric and integer. For example:

x<-(1:5)
class(x)
[1] "integer"

x<-c(1,2,3,4,5)
class(x)
[1] "numeric"
  • Why does this occur?
  • Which vector to use at the expense of another and, on account of which aspect (advantages and disadvantages)?
  • What is the relation of a type variable double with these previous classes?
  • Is there any connection between the concept of "floating point" and these classes?

1 answer

5


Why does this happen?

The R has two classes of numbers: integer and Numeric. The class integer is only used to record whole numbers, while Numeric serves to record real numbers (although, if I wanted to use a more accurate language, the correct is to say that they are floating point numbers with double precision).

By default, vectors built with : sane integer, as vectors built with c are built as Numeric. This is so because it is so. It was necessary to choose a way to standardize the creation of vectors. In addition, most mathematical operations on computers are done using floating-point arithmetic, then do the function c create vectors of this class prevents vectors integer have to be converted to Numeric when any operation that is not defined in the integers has to be performed.

However, it is possible to use the function c to construct a vector with integers:

x <- c(1L, 2L, 3L, 4L, 5L)
class(x)
[1] "integer"

The character L after a number indicates to the R that we wish this number to be recorded in memory as an integer.

Which vector to use at the expense of another? Because of what aspect (advantages and disadvantages)?

For the vast majority of cases, it doesn’t matter. The class integer takes up less memory space:

x <- 1:100
object.size(x)
448 bytes
object.size(as.numeric(x))
848 bytes

In this way, there may be extreme situations where the economy caused by the use of integer if justified, but it is not something I would worry about if my programs are running in the memory available on my machine.

Thus, although integer occupy less memory, this class can not represent as many different numbers as Numeric:

.Machine$integer.max
[1] 2147483647
.Machine$double.xmax
1.797693e+308

See that on my PC, the largest whole R can represent is 2147483647 (two billion and little), while the largest number of floating point is much larger and I don’t even think it has an official name. This limit for integers is very easy to check:

as.integer(2147483647)
[1] 2147483647
as.integer(2147483647+1)
[1] NA

Note that up to 2147483647 it is possible to store an integer value in the memory of the R, but if I add a unit to it, an error occurs precisely because the limit has been exceeded.

Therefore, although the integers occupy less memory space, it may not be a good idea to use them if some calculation goes beyond 2147483647 or there are mathematical operations that can go beyond the domain of the integers, such as division, for example.

What is the relationship of a double-type variable with these previous classes? Is there any connection between the concept of "floating point" and these classes?

I believe I have answered this question within the previous ones.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.