round_any 相当于 dplyr?

round_any equivalent for dplyr?

我正在尝试切换到 "new" tidyverse ecosystem and try to avoid loading the old packages from Wickham et al. I used to rely my coding previously. I found round_any function from plyr 在许多情况下很有用,在这些情况下我需要对绘图、表格等进行自定义舍入。例如

x <- c(1.1, 1.0, 0.99, 0.1, 0.01, 0.001) 

library(plyr)    

round_any(x, 0.1, floor)
# [1] 1.1 1.0 0.9 0.1 0.0 0.0

tidyverse 中的 plyr 包中是否有 round_any 函数的等效项?

ggplot::cut_width 正如其中一条评论所指出的那样, return 甚至不是一个数字向量,而是一个因子。所以它不是真正的替代品。

由于 round 而不是 floor 是默认的舍入方法,自定义替换直到(dplyr 解决方案可能到达)将是

round_any = function(x, accuracy, f=round){f(x/ accuracy) * accuracy}

此方法也可以直接从 plyr package which contains this implementation. However, be careful when loading plyr into a workspace which will cause naming conflicts when using dplyr 中使用。