Hi list, The function is.integer tests if an object is of type integer: see e.g.: is.integer(12) # FALSE is.real(12) # TRUE mode(12) # "numeric" But how can I test if a number is actually an integer? R seek is difficult to search in this case because it mainly yields entries about the integer()-function family. Thanks for any hint! Christoph Heibl
On 9/4/07, Christoph Heibl <christoph.heibl at gmx.net> wrote:> Hi list, > > The function is.integer tests if an object is of type integer: > > see e.g.: > > is.integer(12) # FALSE > is.real(12) # TRUE > mode(12) # "numeric" > > But how can I test if a number is actually an integer? R seek is > difficult to search in this case because it mainly yields entries > about the integer()-function family. > > Thanks for any hint! > Christoph Heibl > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >How about x<- c(12,12.66) trunc(x)==x ? -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
On 04-Sep-07 09:53:43, Christoph Heibl wrote:> Hi list, > > The function is.integer tests if an object is of type integer: > > see e.g.: > > is.integer(12) # FALSE > is.real(12) # TRUE > mode(12) # "numeric" > > But how can I test if a number is actually an integer? R seek is > difficult to search in this case because it mainly yields entries > about the integer()-function family. > > Thanks for any hint! > Christoph HeiblIn infer you want to test whether the real x has an integer value. ((x-floor(x))==0) would do it (covers negative integers as well), though there may well be a smarter way. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 04-Sep-07 Time: 11:11:46 ------------------------------ XFMail ------------------------------
>>>>> "CH" == Christoph Heibl <christoph.heibl at gmx.net> >>>>> on Tue, 4 Sep 2007 11:53:43 +0200 writes:CH> Hi list, CH> The function is.integer tests if an object is of type integer: CH> see e.g.: CH> is.integer(12) # FALSE CH> is.real(12) # TRUE CH> mode(12) # "numeric" CH> But how can I test if a number is actually an integer? something like round(x) == x is often good enough, maybe x %% 1 == 0 seems a bit more efficient. Note that both return NA whenever x[] is NA so may not directly be appropriate for your use case. CH> R seek is difficult to search in this case because it mainly yields entries CH> about the integer()-function family. "R seek" ??? Do you mean the R function RSiteSearch() which goes to 'http://search.r-project.org/' ? Well, calling RSiteSearch("integer number") gives almost 3000 hits, *but* number 10 is exactly relevant to your question... CH> Thanks for any hint! CH> Christoph Heibl
> On 9/4/07, Christoph Heibl <christoph.heibl at gmx.net> wrote: >> Hi list, >> >> The function is.integer tests if an object is of type integer: >> >> see e.g.: >> >> is.integer(12) # FALSE >> is.real(12) # TRUE >> mode(12) # "numeric" >> >> But how can I test if a number is actually an integer? R seek is >> difficult to search in this case because it mainly yields entries >> about the integer()-function family. >> >> Thanks for any hint! >> Christoph Heibl > ?mode > r = 12 > is.integer(r)[1] FALSE> is.double(r)[1] TRUE> i = as.integer(r) > storage.mode(r)[1] "double"> storage.mode(i)[1] "integer" Paolo Sonego