Dear R-help - I have thought about this question for a bit, and come up with no satisfactory answer. Say I have the numeric vector t1, given as t1 <- c(1.0, 1.5, 2.0, 2.5, 3.0) I simply want to reliably extract the unique integers from t1, i.e., the vector c(1, 2, 3). This is of course superficially simple to carry out. However, my question is related to R FAQ 7.31, "Why doesn't R think these numbers are equal?" The first sentence of that FAQ reads, "The only numbers that can be represented exactly in R's numeric type are integers and fractions whose denominator is a power of 2." All the methods I've devised to do the above task seem to ultimately rely on the fact that identical(x.0, x) == TRUE, for integer x. My assumption, which I'm hoping can be verified, is that, for example, 2.0 (when, say, entered at the prompt and not computed from an algorithm) is an integer in the sense of FAQ 7.31. This seems to be the case on my machine. > identical(2.0, 2) [1] TRUE Apologies that this is such a trivial question, it seems so obvious on the surface, I just want to be sure I am understanding it correctly. Erik Iverson iverson at biostat.wisc.edu sessionInfo() R version 2.7.0 (2008-04-22) i686-pc-linux-gnu locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] grDevices datasets tcltk splines graphics utils stats [8] methods base other attached packages: [1] fortunes_1.3-4 debug_1.1.0 mvbutils_1.1.1 SPLOTS_1.3-47 [5] Hmisc_3.4-3 chron_2.3-21 survival_2.34-1 erik_0.0-1 loaded via a namespace (and not attached): [1] cluster_1.11.10 grid_2.7.0 lattice_0.17-6
on 05/16/2008 09:56 AM Erik Iverson wrote:> Dear R-help - > > I have thought about this question for a bit, and come up with no > satisfactory answer. > > Say I have the numeric vector t1, given as > > t1 <- c(1.0, 1.5, 2.0, 2.5, 3.0) > > I simply want to reliably extract the unique integers from t1, i.e., the > vector c(1, 2, 3). This is of course superficially simple to carry out.Use modulo division: > t1[t1 %% 1 == 0] [1] 1 2 3 or > unique(t1[t1 %% 1 == 0]) [1] 1 2 3> However, my question is related to R FAQ 7.31, "Why doesn't R think > these numbers are equal?" The first sentence of that FAQ reads, "The > only numbers that can be represented exactly in R's numeric type are > integers and fractions whose denominator is a power of 2." > > All the methods I've devised to do the above task seem to ultimately > rely on the fact that identical(x.0, x) == TRUE, for integer x. > > My assumption, which I'm hoping can be verified, is that, for example, > 2.0 (when, say, entered at the prompt and not computed from an > algorithm) is an integer in the sense of FAQ 7.31. > > This seems to be the case on my machine. > > > identical(2.0, 2) > [1] TRUE > > Apologies that this is such a trivial question, it seems so obvious on > the surface, I just want to be sure I am understanding it correctly.Keep in mind that by default and unless specifically coerced to integer, numbers in R are double precision floats: > is.integer(2) [1] FALSE > is.numeric(2) [1] TRUE > is.integer(2.0) [1] FALSE > is.numeric(2.0) [1] TRUE So: > identical(2.0, as.integer(2)) [1] FALSE Does that help? Marc Schwartz
2, 2.0, 2e0 are all double while 2L is an integer. On Fri, May 16, 2008 at 10:56 AM, Erik Iverson <iverson at biostat.wisc.edu> wrote:> Dear R-help - > > I have thought about this question for a bit, and come up with no > satisfactory answer. > > Say I have the numeric vector t1, given as > > t1 <- c(1.0, 1.5, 2.0, 2.5, 3.0) > > I simply want to reliably extract the unique integers from t1, i.e., the > vector c(1, 2, 3). This is of course superficially simple to carry out. > > However, my question is related to R FAQ 7.31, "Why doesn't R think these > numbers are equal?" The first sentence of that FAQ reads, "The only numbers > that can be represented exactly in R's numeric type are integers and > fractions whose denominator is a power of 2." > > All the methods I've devised to do the above task seem to ultimately rely on > the fact that identical(x.0, x) == TRUE, for integer x. > > My assumption, which I'm hoping can be verified, is that, for example, 2.0 > (when, say, entered at the prompt and not computed from an algorithm) is an > integer in the sense of FAQ 7.31. > > This seems to be the case on my machine. > >> identical(2.0, 2) > [1] TRUE > > Apologies that this is such a trivial question, it seems so obvious on the > surface, I just want to be sure I am understanding it correctly. > > Erik Iverson > iverson at biostat.wisc.edu > > sessionInfo() > R version 2.7.0 (2008-04-22) > i686-pc-linux-gnu > > locale: > LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C > > attached base packages: > [1] grDevices datasets tcltk splines graphics utils stats > [8] methods base > > other attached packages: > [1] fortunes_1.3-4 debug_1.1.0 mvbutils_1.1.1 SPLOTS_1.3-47 > [5] Hmisc_3.4-3 chron_2.3-21 survival_2.34-1 erik_0.0-1 > > loaded via a namespace (and not attached): > [1] cluster_1.11.10 grid_2.7.0 lattice_0.17-6 > > ______________________________________________ > R-help at r-project.org 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. >