Prof Brian D Ripley
2002-May-29 11:48 UTC
[R] Why is.integer() doesn't work with single values?
On Wed, 29 May 2002, Sven Garbade wrote:> Hi all, > > I don't understand the behavior of is.integer(): > > > x <- integer() > > is.integer(x) > [1] TRUE > > x <- 10 > > is.integer(x) > [1] FALSE > > x <- 1:10 > > is.integer(x) > [1] TRUE > > Why is.interger() returns FALSE if x has only one element? And how can > someone check if x is an integer but contains only one value? (R 1.5.0 > on Linux i386)Your guesses are wrong:> is.integer(as.integer(10))[1] TRUE> is.integer(as.numeric(1:10))[1] FALSE If all else (except posting to R-help) fails, read the documentation which says `is.integer' returns `TRUE' or `FALSE' depending on whether its argument is of integer type or not. Note *NOT* `is an integer'. To test for an integer *value* you could use x == round(x), assuming that you want false for a value that is not integer by rounding error. Otherwise use all.equal. -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-May-29 11:59 UTC
[R] Why is.integer() doesn't work with single values?
Sven Garbade <garbade at psy.uni-muenchen.de> writes:> Hi all, > > I don't understand the behavior of is.integer(): > > > x <- integer() > > is.integer(x) > [1] TRUE > > x <- 10 > > is.integer(x) > [1] FALSE > > x <- 1:10 > > is.integer(x) > [1] TRUE > > Why is.interger() returns FALSE if x has only one element? And how can > someone check if x is an integer but contains only one value? (R 1.5.0 > on Linux i386)This has nothing to do with is.integer or lengths. It is the ":" operator that tries to make its result an integer vector whenever possible. Constant are generally floating point numbers, even when they happen to be integers. Try is.integer(1:1) and is.integer(c(1,2,3)). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi all, I don't understand the behavior of is.integer():> x <- integer() > is.integer(x)[1] TRUE> x <- 10 > is.integer(x)[1] FALSE> x <- 1:10 > is.integer(x)[1] TRUE Why is.interger() returns FALSE if x has only one element? And how can someone check if x is an integer but contains only one value? (R 1.5.0 on Linux i386) Thanks, Sven -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, On Wed, 29 May 2002, Sven Garbade wrote: |Hi all, | |I don't understand the behavior of is.integer(): | |> x <- integer() |> is.integer(x) |[1] TRUE |> x <- 10 |> is.integer(x) |[1] FALSE This is OK -- x is now 10 (defaults to float), not any more integer(). Note that assignment deleted the previous definition. |> x <- 1:10 |> is.integer(x) |[1] TRUE 1:10 defaults to an integer vector, so your new vector is (a vector of) integer(s). |Why is.interger() returns FALSE if x has only one element? And how can |someone check if x is an integer but contains only one value? (R 1.5.0 |on Linux i386) length(x) gives the number of values. is.integer() gives the type of argument (the way the argument is presented in memory). E.g:> x <- 10 > is.integer(x)[1] FALSE> x == as.integer(x)# use rather abs(x - as.integer(x)) < sqrt(Machine()$double.eps) # or something similar. [1] TRUE> length(x)[1] 1 Perhaps it helps. Ott -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Sven Garbade <garbade at psy.uni-muenchen.de> writes:>Hi all, > >I don't understand the behavior of is.integer(): > >> x <- integer() >> is.integer(x) >[1] TRUE >> x <- 10 >> is.integer(x) >[1] FALSE >> x <- 1:10 >> is.integer(x) >[1] TRUE > >Why is.interger() returns FALSE if x has only one element? And how can >someone check if x is an integer but contains only one value? (R 1.5.0 >on Linux i386)That is down to the storage mode. Try: x <- 10 typeof(x) y <- 1:10 typeof(y) You can test with: x == as.integer(x) Mark -- Mark Myatt -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._