Dear usRers, I want to judge virable is or not a integer? e.g. is.integer(1) FALSE because it is a numeric, but i want it's true. as.integer may not be used. because i don't know a is 1 or 1.1. -- PO SU mail: [hidden email] Majored in Statistics from SJTU ______________________________________________ [hidden email] 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. |
On 18-10-2014, at 12:41, PO SU <[hidden email]> wrote: > > > Dear usRers, > I want to judge virable is or not a integer? > e.g. is.integer(1) FALSE because it is a numeric, but i want it's true. > as.integer may not be used. because i don't know a is 1 or 1.1. > is.integer is surely what you need if you wish to test if a variable is integer. See this # a <- 1 # b <- 1L # is.integer(a) [1] FALSE # is.integer(b) [1] TRUE See the help for is.integer to see how you can test for a wholenumber, which might be what you want. Berend ______________________________________________ [hidden email] 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. |
But i use a<-10/b , b is some value ,may be 5, maybe 5.5 not in the form xxL ,so how can i do in the situation to judge a ? -- PO SU mail: [hidden email] Majored in Statistics from SJTU At 2014-10-18 18:58:48, "Berend Hasselman" <[hidden email]> wrote: > >On 18-10-2014, at 12:41, PO SU <[hidden email]> wrote: > >> >> >> Dear usRers, >> I want to judge virable is or not a integer? >> e.g. is.integer(1) FALSE because it is a numeric, but i want it's true. >> as.integer may not be used. because i don't know a is 1 or 1.1. >> > >is.integer is surely what you need if you wish to test if a variable is integer. >See this > ># a <- 1 ># b <- 1L > ># is.integer(a) >[1] FALSE > ># is.integer(b) >[1] TRUE > >See the help for is.integer to see how you can test for a wholenumber, which might be what you want. > >Berend > [hidden email] 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. |
> But i use a<-10/b , b is some value ,may be 5, maybe 5.5
If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0. See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding. S ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}} ______________________________________________ [hidden email] 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. |
It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply, when i want to know 1 is an integer, there seems no easy way to get the answer. So, is there anyone happen to know it? -- PO SU mail: [hidden email] Majored in Statistics from SJTU At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: >> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 >If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0. > >See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding. > >S > >******************************************************************* >This email and any attachments are confidential. Any use, copying or >disclosure other than by the intended recipient is unauthorised. If >you have received this message in error, please notify the sender >immediately via +44(0)20 8943 7000 or notify [hidden email] >and delete this message and any copies from your computer and network. >LGC Limited. Registered in England 2991879. >Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK [hidden email] 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. |
Don't know if this trivial reply will be useful
>a=5 >is.numeric(a) >[1] TRUE >b="try" >is.numeric(b) >[1] FALSE Il 18/ott/2014 16:29 "PO SU" <[hidden email]> ha scritto: > > It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but > deeply, when i want to know 1 is an integer, there seems no easy way to > get the answer. > So, is there anyone happen to know it? > > > > > -- > > PO SU > mail: [hidden email] > Majored in Statistics from SJTU > > > > At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: > >> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 > >If you do floating point arithmetic on integers you'll usually get > floating point answers, including the 5.0. > > > >See FAQ 7.31 for the usual floating point problem, and ?all.equal for the > usual answer to it. You could see if a result is close to an integer by,for > example, using all.equal to compare it to itself after rounding. > > > >S > > > >******************************************************************* > >This email and any attachments are confidential. Any use, copying or > >disclosure other than by the intended recipient is unauthorised. If > >you have received this message in error, please notify the sender > >immediately via +44(0)20 8943 7000 or notify [hidden email] > >and delete this message and any copies from your computer and network. > >LGC Limited. Registered in England 2991879. > >Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by PO SU
Sorry for my previous hurry misunderstanding.
Try this link: http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer 2014-10-18 16:25 GMT+02:00 PO SU <[hidden email]>: > > It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but > deeply, when i want to know 1 is an integer, there seems no easy way to > get the answer. > So, is there anyone happen to know it? > > > > > -- > > PO SU > mail: [hidden email] > Majored in Statistics from SJTU > > > > At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: > >> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 > >If you do floating point arithmetic on integers you'll usually get > floating point answers, including the 5.0. > > > >See FAQ 7.31 for the usual floating point problem, and ?all.equal for the > usual answer to it. You could see if a result is close to an integer by,for > example, using all.equal to compare it to itself after rounding. > > > >S > > > >******************************************************************* > >This email and any attachments are confidential. Any use, copying or > >disclosure other than by the intended recipient is unauthorised. If > >you have received this message in error, please notify the sender > >immediately via +44(0)20 8943 7000 or notify [hidden email] > >and delete this message and any copies from your computer and network. > >LGC Limited. Registered in England 2991879. > >Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by PO SU
Further and last trial:
> a=5.102> a-floor(a)==0[1] FALSE> a=5.9> a-floor(a)==0[1] FALSE> a=19> a-floor(a)==0[1] TRUE All the best, Sergio 2014-10-18 16:25 GMT+02:00 PO SU <[hidden email]>: > > It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but > deeply, when i want to know 1 is an integer, there seems no easy way to > get the answer. > So, is there anyone happen to know it? > > > > > -- > > PO SU > mail: [hidden email] > Majored in Statistics from SJTU > > > > At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: > >> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 > >If you do floating point arithmetic on integers you'll usually get > floating point answers, including the 5.0. > > > >See FAQ 7.31 for the usual floating point problem, and ?all.equal for the > usual answer to it. You could see if a result is close to an integer by,for > example, using all.equal to compare it to itself after rounding. > > > >S > > > >******************************************************************* > >This email and any attachments are confidential. Any use, copying or > >disclosure other than by the intended recipient is unauthorised. If > >you have received this message in error, please notify the sender > >immediately via +44(0)20 8943 7000 or notify [hidden email] > >and delete this message and any copies from your computer and network. > >LGC Limited. Registered in England 2991879. > >Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by PO SU
It sounds like you want an 'is.integral' function to tell if a
number acts like a mathematical integer, as opposed to 'is.integer', which tells if a number is stored as a 32-bit computer integer. The test will depend on what properties of mathematical integers you are most interested in. is.integral <- function (x) (floor(x) == x) & (abs(x) + 1 > abs(x)) will return TRUE if x has no fractional part and the number's putative successor (predecessor if negative) is different than the number. That latter test is equivalent (roughly) to log2(abs(x))<53 and comes into play when you run out of bits in the mantissa of a double precision number. (One might want it to return NA in that case, but I think FALSE works better.) Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Oct 18, 2014 at 3:41 AM, PO SU <[hidden email]> wrote: > > > Dear usRers, > I want to judge virable is or not a integer? > e.g. is.integer(1) FALSE because it is a numeric, but i want it's true. > as.integer may not be used. because i don't know a is 1 or 1.1. > > > > > > -- > > PO SU > mail: [hidden email] > Majored in Statistics from SJTU > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by fonda99
Tks for your help, after investigate in your link, i find there seems three ways can be adoped: 1. is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol) e.g. is.wholenumber(1) 2. x%%1==0 3. all.equal(a, as.integer(a)) and also included your last suggestion using floor. and also tks for other helpers！ -- PO SU mail: [hidden email] Majored in Statistics from SJTU At 2014-10-18 22:48:15, "Sergio Fonda" <[hidden email]> wrote: Sorry for my previous hurry misunderstanding. Try this link: http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer 2014-10-18 16:25 GMT+02:00 PO SU <[hidden email]>: It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply, when i want to know 1 is an integer, there seems no easy way to get the answer. So, is there anyone happen to know it? -- PO SU mail: [hidden email] Majored in Statistics from SJTU At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: >> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 >If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0. > >See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding. > >S > >******************************************************************* >This email and any attachments are confidential. Any use, copying or >disclosure other than by the intended recipient is unauthorised. If >you have received this message in error, please notify the sender >immediately via +44(0)20 8943 7000 or notify [hidden email] >and delete this message and any copies from your computer and network. >LGC Limited. Registered in England 2991879. >Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
3. all.equal(a, as.integer(a))
Note that this one tests if 'a' can be stored accurately as a 32-bit signed integer. If you want to know if 'a' can be used as an accurate count, then you want to test if a+1>a (use abs() in case a is negative). E.g., try this for a<-2^49-1, about 5*10^14. You have to decide what properties of integers you are interested in. Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Oct 18, 2014 at 10:02 AM, PO SU <[hidden email]> wrote: > > Tks for your help, after investigate in your link, i find there seems three ways can be adoped: > 1. is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol) > e.g. is.wholenumber(1) > 2. x%%1==0 > > > 3. all.equal(a, as.integer(a)) > > > and also included your last suggestion using floor. and also tks for other helpers！ > > > > > > > > > -- > > PO SU > mail: [hidden email] > Majored in Statistics from SJTU > > At 2014-10-18 22:48:15, "Sergio Fonda" <[hidden email]> wrote: > > > > Sorry for my previous hurry misunderstanding. > Try this link: > http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer > > > > > 2014-10-18 16:25 GMT+02:00 PO SU <[hidden email]>: > > > > It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply, when i want to know 1 is an integer, there seems no easy way to get the answer. > > So, is there anyone happen to know it? > > > > > > > > > > -- > > > > PO SU > > mail: [hidden email] > > Majored in Statistics from SJTU > > > > > > > > At 2014-10-18 20:10:09, "S Ellison" <[hidden email]> wrote: > >>> But i use a<-10/b , b is some value ,may be 5, maybe 5.5 > >>If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0. > >> > >>See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding. > >> > >>S > >> > >>******************************************************************* > >>This email and any attachments are confidential. Any use, copying or > >>disclosure other than by the intended recipient is unauthorised. If > >>you have received this message in error, please notify the sender > >>immediately via +44(0)20 8943 7000 or notify [hidden email] > >>and delete this message and any copies from your computer and network. > >>LGC Limited. Registered in England 2991879. > >>Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > > ______________________________________________ > > [hidden email] 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. > > > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by PO SU
> It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply,
> when i want to know 1 is an integer, there seems no easy way to get the > answer. > So, is there anyone happen to know it? First, you are not being as clear as you may think when you say " when i want to know 1 is an integer". It isn't. 1L is an integer. 1 is floating point. Do you want to know whether something is _stored as_ integer (eg 2L), whether it is a floating point number with exactly no nonzero digits after the point (eg 2.0), or whether it is something which would normally be expected to be integer if represented to infinite precision but is not exactly represented because of finite machine precision (eg sqrt(2)^2)? Once you've sorted out which of those you want - I think the last of the three - please read the posts you’re replying to. all.equal() was the suggested answer and is likely to be the nearest to a reliable answer you will get. Almost anything else will at least sometimes fail; for example sqrt(4.0) == 2L # [1] TRUE #But sqrt(2)^2 == 2L #[1] FALSE #whereas all.equal(sqrt(2)^2, 2L) #[1] TRUE Thus, all.equal can be used to test for something that would normally be considered an integer within machine precision, for example using nearly.integer <- function(x) all.equal(x, round(x)) You may make the comparison closer to a 'within machine precision' comparison by amending the tol argument to all.equal, which is documented on the help page you were referred to. S Ellison ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify [hidden email] and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK ______________________________________________ [hidden email] 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. |
In reply to this post by PO SU
> 3. all.equal(a, as.integer(a))
Closer, but be aware that all.equal will not always return TRUE or FALSE and - more importantly - as.integer truncates towards zero and does NOT generally round to the nearest integer. a <- 4 - sqrt(2)^2 #Analytically 2 all.equal(a, as.integer(a)) # [1] "Mean relative difference: 0.5" #because as.integer(a) # [1] 1 To return FALSE from all.equal, wrap it in something like if(all.equal(a, round(a))==TRUE) TRUE else FALSE S ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify [hidden email] and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |