What is the difference between numeric and integer vectors?

6

In R, identical vectors (containing the same numbers) can assume the numeric and integer classes. 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 over another and for what aspect (advantages and disadvantages)?
  • What is the relation of a variable type double with these previous classes?
  • Is there any connection between the concept of "floating point" and these classes?
asked by anonymous 19.10.2018 / 03:30

1 answer

5
  

Why does this occur?

The R has two classes of numbers: integer and numeric. The integer class is just for registering integers, whereas numeric serves to record real numbers (although, if I wanted to use more precise language, floating point with double precision).

By default, vectors constructed with : are integer , while vectors constructed with c are built as numeric . It 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, so doing the c function creates vectors of this class prevents integer vectors from being converted to numeric when some operation that is not defined in the integers has to be performed.

However, you can use the c function 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 R that we want this number to be registered in memory as an integer.

  

Which vector to use over another? On account of what aspect (advantages and disadvantages)?

For the immense majority of cases, whatever. The integer class 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 in which the economy caused by the use of integer is justified, but it is not something that I would worry about if my programs are running in the available memory on my machine .

So, although integer has 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 integer that R can represent is 2147483647 (two billion and a few), while the largest floating point number is much larger and I do not think it even has an official name. This limit for integers is quite easy to verify:

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

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

So while integers occupy less space in memory, it might not be a good idea to use them if some calculation goes beyond 2147483647 or there are mathematical operations that can exit the integer domain, such as division, for example. p>

  

What is the relation 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.

    
19.10.2018 / 12:25