Hi, All I am using is.integer function to examine whether an object is an integer or not, but I get such results,> x<-3 > is.integer(x)[1] FALSE> x<-3:4 > x[1] 3 4> is.integer(x)[1] TRUE Seems that the is.integer cannot handle scalers,> is.integer(5)[1] FALSE> is.integer(5:6)[1] TRUE Is this a bug in R or I made some mistakes? I am using R 2.2.1 under Windows XP Thanks a lot! Leon [[alternative HTML version deleted]]
Leon <amstat2006 <at> gmail.com> writes:> > Hi, All > I am using is.integer function to examine whether an object is an integer ornot, but I get such results,> > > x<-3 > > is.integer(x) > [1] FALSEx <- 3 typeof(3) [1] "double" This may not be a wise decision in hindsight, but probably was made to avoid conversion when in the next step you do x[2] = 1.5 y <- as.integer(3) typeof(y) [1] "integer"> > x<-3:4 > > x > [1] 3 4 > > is.integer(x) > [1] TRUEHere R seems to know that 3 is an integer, which I believe is a bit inconsistent, but wise, because mostly you use integers here. Mostly; because 1.5:3.5 gives a reasonable result [1] 1.5 2.5 3.5 Dieter
On Wed, 26 Apr 2006, Leon wrote:> Hi, All > I am using is.integer function to examine whether an object is an integer or not, but I get such results,As the help page says, is.integer() Creates or tests for objects of type '"integer"'. That is, is.integer() tests to see whether the variable is of type integer, not whether the number currently stored in that variable is a whole number. Numerical constants such as 5 are of type double, but the results of : with whole number arguments are of type integer. -thomas> >> x<-3 >> is.integer(x) > [1] FALSE > >> x<-3:4 >> x > [1] 3 4 >> is.integer(x) > [1] TRUE > > Seems that the is.integer cannot handle scalers, > >> is.integer(5) > [1] FALSE > >> is.integer(5:6) > [1] TRUE > > Is this a bug in R or I made some mistakes? I am using R 2.2.1 under Windows XP > > Thanks a lot! > > Leon > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Not to belabor the point, but if you really need to distinguish integers from other numerical constants, you could try something like this: > x <- as.integer(5) > typeof(x) [1] "integer" > is.integer(x) [1] TRUE > on 4/26/2006 1:20 AM Leon said the following:> Hi, All > I am using is.integer function to examine whether an object is an integer or not, but I get such results, > > >> x<-3 >> is.integer(x) >> > [1] FALSE > > >> x<-3:4 >> x >> > [1] 3 4 > >> is.integer(x) >> > [1] TRUE > > Seems that the is.integer cannot handle scalers, > > >> is.integer(5) >> > [1] FALSE > > >> is.integer(5:6) >> > [1] TRUE > > Is this a bug in R or I made some mistakes? I am using R 2.2.1 under Windows XP > > Thanks a lot! > > Leon > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >-- Michael Prager, Ph.D. Southeast Fisheries Science Center NOAA Center for Coastal Fisheries and Habitat Research Beaufort, North Carolina 28516 ** Opinions expressed are personal, not official. No ** official endorsement of any product is made or implied.