I am trying to figure out R data types and/or storage mode. For example:> #From a clean workspace > gc()used (Mb) gc trigger (Mb) Ncells 415227 11.1 597831 16 Vcells 103533 0.8 786432 6> x <- seq(0,100000,1) > is.integer(x)[1] FALSE> is.double(x)[1] TRUE> object.size(x)[1] 800036> gc()used (Mb) gc trigger (Mb) Ncells 415247 11.1 667722 17.9 Vcells 203543 1.6 786432 6.0> x <- as.integer(x) > is.integer(x)[1] TRUE> is.double(x)[1] FALSE> gc()used (Mb) gc trigger (Mb) Ncells 415249 11.1 741108 19.8 Vcells 153543 1.2 786432 6.0> x <- 1:100000 > is.integer(x)> gc()used (Mb) gc trigger (Mb) Ncells 415278 11.1 741108 19.8 Vcells 153553 1.2 786432 6.0> is.integer(3)[1] FALSE> is.double(3)[1] TRUE> is.integer(3 * as.integer(5))[1] FALSE> is.integer(as.integer(3) * as.integer(5))[1] TRUE> is.integer(c(as.integer(5),as.integer(6),as.integer(7)))[1] TRUE> is.integer(c(as.integer(5),as.integer(6),7))[1] FALSE> is.integer(seq(as.integer(5),as.integer(10),1))[1] FALSE> is.integer(seq(as.integer(5),as.integer(10),as.integer(1)))[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" == Benjamin STABLER <Benjamin.STABLER at odot.state.or.us> >>>>> on Wed, 14 Jan 2004 15:52:46 -0800 writes:<........> 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:> I am trying to figure out R data types and/or storage mode. For example:[...]> 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.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> x <- seq(10) > storage.mode(x)[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. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595