[R] 2 small problems: integer division and the nature of NA

Spencer Graves spencer.graves at pdf.com
Fri Feb 4 17:43:02 CET 2005


      It's the difference between integers and reals:  48 and 24 are 
integers;  4.8 and 0.2 are floating point numbers.  Consider: 

 > (4.8+.Machine$double.eps) %/% (0.2-.Machine$double.eps)
[1] 24
 > (4.8-.Machine$double.eps) %/% (0.2+.Machine$double.eps)
[1] 23
 >
      Does this help?  spencer graves

Denis Chabot wrote:

> Hi,
>
> I'm wondering why
>
> 48 %/% 2 gives 24
> but
> 4.8 %/% 0.2 gives 23...
> I'm not trying to round up here, but to find out how many times 
> something fits into something else, and the answer should have been 
> the same for both examples, no?
>
> On a different topic, I like the behavior of NAs better in R than in 
> SAS (at least they are not considered the smallest value for a 
> variable), but at the same time I am surprised that the sum of NAs is 
> 0 instead of NA.
>
> The sum of a vector having at least one NA but also valid data gives 
> NA if we do not specify na.rm=T. But with na.rm=T, we are telling sum 
> to give the sum of valid data, ignoring NAs that do not tell us 
> anything about the value of a variable. I found out while getting the 
> sum of small subsets of my data (such as when subsetting by several 
> variables), sometimes a "cell" only contained NAs for my response 
> variable. I would have expected the sum to be NA in such cases, as I 
> do not have a single data point telling me the value of my response 
> here. But R tells me the sum was zero in that cell! Was this behavior 
> considered "desirable" when sum was built? If not, any hope it will be 
> fixed?
>
> Sincerely,
>
> Denis Chabot
>
> ______________________________________________
> 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




More information about the R-help mailing list