Skip to content

Calculate remainer

5 messages · livia, Chuck Cleland, Julian Burgos +1 more

#
livia wrote:
?"%%" to see how to get the remainder.  You might put the "result" and
remainder together in a function like this:

myfunc <- function(x,y){list(result = floor(x / y), remainder = x %% y)}

myfunc(x=50, y=12)
$result
[1] 4

$remainder
[1] 2
#
[1] 2
[1] 4

        
--- livia <yn19832 at msn.com> wrote:

            
http://www.nabble.com/Calculate-remainer-tp14414906p14414906.html
#
Hi Livia,

There are several ways to do this.  Try:

a=50/12

floor(a) will give you the entire portion, and

a-floor(a) will give you the remainder.

Julian
livia wrote:
#
This is OK if the ratio is positive, but for -50
divided by 12 the floor is -5 and the remainder is 10
(and not -4 and -2 as one may want). By the way, using
%% and %/% leads to same result.
Using trunc will remedy the situation, i.e.
[1] -4
[1] -2
--- Julian Burgos <jmburgos at u.washington.edu> wrote: