[R] "rounding" to next lowest/highest 'x%%y' (mod) equals zero?

Johannes Graumann johannes_graumann at web.de
Fri Aug 24 15:15:12 CEST 2007


Dear smart ones,

Image a vector 'test' that looks like so:

0
0.5
1.5
2.1
3.1
4

I'm looking for an efficient function that would do something like

moddown <- function(test,integer){
  for (element %in% test){
    if (element%%integer != 0) {
      "return next lowest integer for which '%%' returns 0"
    } else {
      "return element"
    }
  }
}

and return for 'test' a vector like so:

0
0
0
2
2
4

My current solution like outlined above seems very inelegant ...

Thanks for any hints, Joh



More information about the R-help mailing list