In R, when a vector becomes "too long"?

5

When trying to create an infinite vector ( 1:Inf ) I received the following message:

  

Error in 1: Inf: result would be too long to vector

However, when the vector does not know in memory the message is usually different. The code below creates the three situations: a) can create the vector, b) does not fit in memory and c) "too long".

object.size(1:1e9) # limite superior do tipo a
# 4000000040 bytes

object.size(1:1e10) # limite inferior do tipo b
# Error: cannot allocate vector of size 74.5 Gb

...

object.size(1:1e15) # limite superior do tipo b
# Error: cannot allocate vector of size 7450580.6 Gb

object.size(1:1e16) # limite inferior do tipo c
# Error in 1:1e+16 : result would be too long a vector

Question: Since the two vectors (type b and type c) would not fit in memory, how does R define it to fall into one case or the other?

    
asked by anonymous 02.03.2017 / 18:18

1 answer

4

This has to do with the source code of R. See that the : function is set to C here . There, you may find that this error appears in this condition:

 double r = fabs(n2 - n1);
 if(r >= R_XLEN_T_MAX)
     errorcall(call, _("result would be too long a vector"));

This constant R_XLEN_T_MAX in turn is set here .

# define R_XLEN_T_MAX 4503599627370496

That is, if the number is greater than 4503599627370496 , the error will occur. Now see that:

> 4503599627370496 > 1e15
[1] TRUE
> 4503599627370496 > 1e16
[1] FALSE

See also this here:

> k <- 1:4503599627370496
Error: cannot allocate vector of size 33554432.0 Gb
> k <- 1:4503599627370497
Error in 1:4503599627370497 : result would be too long a vector
    
02.03.2017 / 20:51