round_any等同于dplyr? [英] round_any equivalent for dplyr?
问题描述
我正在尝试切换到新" tidyverse
生态系统,并尝试避免加载旧版本Wickham等人提供的软件包.我以前曾经依赖于我的编码.我发现 round_any
函数来自 plyr 在许多需要对图,表等进行自定义舍入的情况下很有用.例如
I am trying to make a switch to the "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 useful in many cases where I needed custom rounding for plots, tables, etc. E.g.
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
功能是否等效?
Is there an equivalent for round_any
function from plyr package in tidyverse
?
推荐答案
ggplot::cut_width
甚至不返回数字矢量,而是返回一个因子.因此,这不是真正的替代品.
ggplot::cut_width
as pointed to in one of the comments, does not even return a numeric vector, but a factor instead. So it is no real substitute.
由于默认的取整方法是round
而不是floor
,因此将进行自定义替换,直到(dplyr解决方案可能到达)
Since round
and not floor
is the default rounding method, a custom replacement until a (dplyr solution may arrive) would be
round_any = function(x, accuracy, f=round){f(x/ accuracy) * accuracy}
也可以直接从使用dplyr时的命名冲突.
This method could also be used directly from the plyr package which contains this implementation. However, be careful when loading plyr
into a workspace which will cause naming conflicts when using dplyr as well.
这篇关于round_any等同于dplyr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!