Hello everyone, I have got a question about a simple calculation. If I would like to calculate 50/12 and return the result as 4 and the remainer 2. Is there a function of doing this? Many thanks. -- View this message in context: http://www.nabble.com/Calculate-remainer-tp14414906p14414906.html Sent from the R help mailing list archive at Nabble.com.
livia wrote:> Hello everyone, > > I have got a question about a simple calculation. If I would like to > calculate 50/12 and return the result as 4 and the remainer 2. Is there a > function of doing this? > > Many thanks.?"%%" 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 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
> 50 %% 12[1] 2> 50 %/% 12[1] 4> ?Arithmetic--- livia <yn19832 at msn.com> wrote:> > Hello everyone, > > I have got a question about a simple calculation. If > I would like to > calculate 50/12 and return the result as 4 and the > remainer 2. Is there a > function of doing this? > > Many thanks. > -- > View this message in context: >http://www.nabble.com/Calculate-remainer-tp14414906p14414906.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. >
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:> Hello everyone, > > I have got a question about a simple calculation. If I would like to > calculate 50/12 and return the result as 4 and the remainer 2. Is there a > function of doing this? > > Many thanks.
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.> x <- -50 > y <- 12 > a <- trunc(x/y) > r <- x - a*y > a[1] -4> r[1] -2 --- Julian Burgos <jmburgos at u.washington.edu> wrote:> 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: > > Hello everyone, > > > > I have got a question about a simple calculation. > If I would like to > > calculate 50/12 and return the result as 4 and the > remainer 2. Is there a > > function of doing this? > > > > Many thanks. > > ______________________________________________ > 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. >