Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vectorized round_up_nice #4

Open
al-obrien opened this issue Jul 2, 2024 · 1 comment
Open

vectorized round_up_nice #4

al-obrien opened this issue Jul 2, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@al-obrien
Copy link
Owner

Use matrix calculations to remove loop. This shows huge improvements when in thousands of values (95% decrease)

round_up_nice <- function(x, nice=c(1,2,4,5,6,8,10)) {

  floor_calc <- 10^floor(log10(x))

  # Find which nice val to use for each input
  exp_m <- outer(floor_calc, nice)
  nice_idx <- apply(x <= exp_m, 1, which.max)

  floor_calc * nice[nice_idx]
}
@al-obrien al-obrien added the enhancement New feature or request label Jul 2, 2024
@al-obrien
Copy link
Owner Author

pmax() may also work, possibly faster, or matrixStats if can suffer for Imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant