Skip to content

Instantly share code, notes, and snippets.

@chadsten
Created November 2, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadsten/5445c9bd132886461f8e59c8ea3f194d to your computer and use it in GitHub Desktop.
Save chadsten/5445c9bd132886461f8e59c8ea3f194d to your computer and use it in GitHub Desktop.
pum_mod2 <- function(conv_factor, value) {
mod <- 0
case_when(
value < conv_factor ~ mod <- ceiling(conv_factor - value),
value %% conv_factor != 0 ~ mod <- ceiling((value / conv_factor) * conv_factor - value),
)
return(mod)
}
pum_mod <- function(conv_factor, value) {
# for values smaller than the conversion factor, we just need add the difference
if (value < conv_factor) {
mod <- ceiling(conv_factor - value)
# for values larger than the conversion factor, we calc the remainder to get up to a number that is evenly divisible
} else if ((value %% conv_factor) != '0') {
mod <- ceiling((value / conv_factor) * conv_factor - value)
} else {
mod <- 0
}
return(mod)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment