[R] data types in R

Mike Prager mike.prager at noaa.gov
Fri Aug 15 21:23:23 CEST 2008


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.



More information about the R-help mailing list