I was having a problem with a little simple function I wrote in R and I think the problem was that R is representing fractional numbers in binary floating point and not decimal notation, so sometimes I was having extra data points counted. Is there a way to cast a number stored in a variable as an integer? -- View this message in context: http://www.nabble.com/data-types-in-R-tp19001443p19001443.html Sent from the R help mailing list archive at Nabble.com.
faq 7.31 On Fri, Aug 15, 2008 at 9:16 AM, Amanda1988 <mandy_zimm at hotmail.com> wrote:> > I was having a problem with a little simple function I wrote in R and I think > the problem was that R is representing fractional numbers in binary floating > point and not decimal notation, so sometimes I was having extra data points > counted. Is there a way to cast a number stored in a variable as an integer? > -- > View this message in context: http://www.nabble.com/data-types-in-R-tp19001443p19001443.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Amanda1988 <mandy_zimm at hotmail.com> wrote:> > I was having a problem with a little simple function I wrote in R and I think > the problem was that R is representing fractional numbers in binary floating > point and not decimal notation, so sometimes I was having extra data points > counted. Is there a way to cast a number stored in a variable as an integer?To store a decimal fraction as the nearest integer, I would start with as.integer(round(x)) where x is the decimal fraction. Almost no computer language stores fraction numbers in decimal. If your code expects such storage, you might consider a different way of coding the problem. If you were to show your R code, someone might suggest a better way of doing thigs. In particular, for regular fractional sequences, it is better practice to generate them as integers, than divide by the common divisor, than to use seq() with fractions. In other cases, it can be helpful to use the length.out= argument to seq(), rather than the to= argument. HTH -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.