Skip to content

R internal data types

3 messages · Benjamin.STABLER@odot.state.or.us, Martin Maechler, Brian Ripley

#
I am trying to figure out R data types and/or storage mode.  For example:
used (Mb) gc trigger (Mb)
Ncells 415227 11.1     597831   16
Vcells 103533  0.8     786432    6
[1] FALSE
[1] TRUE
[1] 800036
used (Mb) gc trigger (Mb)
Ncells 415247 11.1     667722 17.9
Vcells 203543  1.6     786432  6.0
[1] TRUE
[1] FALSE
used (Mb) gc trigger (Mb)
Ncells 415249 11.1     741108 19.8
Vcells 153543  1.2     786432  6.0
used (Mb) gc trigger (Mb)
Ncells 415278 11.1     741108 19.8
Vcells 153553  1.2     786432  6.0
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE

So it looks like R stores numbers as doubles unless the are converted to
integers (long) with the as.integer() function or they are created with the
: operator.  If any of the numbers to a function are not type integer than
the function returns type double.  Is this the case?  Thanks.

Ben Stabler
Oregon Department of Transportation
#
<........>

    Benjamin> So it looks like R stores numbers as doubles
    Benjamin> unless the are converted to integers (long) with
    Benjamin> the as.integer() function or they are created with
    Benjamin> the : operator.  
+/- yes;
In most cases, this should not matter though.

There are a few other functions that return integer ``by
definition'', e.g.,
length(), dim(), nrow(), ncol()  {the latter 3 return 'NULL' or an integer}.


    Benjamin> If any of the numbers to a function are not type
    Benjamin> integer than the function returns type double.  Is
    Benjamin> this the case?

Most functions will return double even when all numeric arguments are
integer.  Also, e.g. length() will return integer() in any case.

Only for some "arithmetic" functions
      (+, -, *, %%, %/%, also min(), max(), sum())
your statement is true.
But really, in most cases, code shouldn't rely on this.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
#
On Wed, 14 Jan 2004 Benjamin.STABLER at odot.state.or.us wrote:

            
[...]
Nearly!

Non-complex `numbers' can have storage mode "double" or "integer".  
Storage mode "integer" is the C `int' type and not the C `long' type, so
probably on all current R platforms doubles are stored in 8 bytes and
integers in 4.

The storage mode is largely under the user/programmer's control: you can 
do

storage.mode(x) <- "double"

for example.  The storage mode of the return value of a function depends 
on how it was programmed, not on the types of its arguments, for example
[1] "integer"

Just a few base functions normally return integer results: you have 
mentioned : and as.integer() and I have illustrated seq().  table() and 
tabulate() are two others I know of, and factors are integer vectors with 
particular attributes.

Current versions of S use integer storage mode much more widely, and it is 
quite possible that in due course more functions in R will make use of it.