我正在考虑使用 mutate_all
中的scales::dollar
格式化表格。
使用sapply
:
>> sapply(mtcars, scales::dollar)
mpg cyl disp hp drat wt qsec vs am gear carb
[1,] "$21.00" "$6" "$160.00" "$110" "$3.90" "$2.62" "$16.46" "$0" "$1" "$4" "$4"
[2,] "$21.00" "$6" "$160.00" "$110" "$3.90" "$2.88" "$17.02" "$0" "$1" "$4" "$4"
[3,] "$22.80" "$4" "$108.00" "$93" "$3.85" "$2.32" "$18.61" "$1" "$1" "$4" "$1"
[4,] "$21.40" "$6" "$258.00" "$110" "$3.08" "$3.22" "$19.44" "$1" "$0" "$3" "$1"
尝试通过dplyr
管道和 scales::dollar
获得相同的结果:
mtcars %>% mutate_all(funs(scales::dollar(.)))
失败:
Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) :
values must be length 1,
but FUN(X[[1]]) result is length 3
可以尝试原始的解决方法:
mtcars %>% mutate_each(funs(as.character(paste0("$", .))))
结果:
>> mtcars %>% mutate_each(funs(as.character(paste0("$", .))))
mpg cyl disp hp drat wt qsec vs am gear carb
1 $21 $6 $160 $110 $3.9 $2.62 $16.46 $0 $1 $4 $4
2 $21 $6 $160 $110 $3.9 $2.875 $17.02 $0 $1 $4 $4
3 $22.8 $4 $108 $93 $3.85 $2.32 $18.61 $1 $1 $4 $1
在a similar discussion,之后,可以轻松地进一步开发方法以创建所需的货币格式,但这不是重点。
为什么scales::dollar(.)
在mutate_all
(或mutate_each
)内应用时失败?什么时候
应用于矢量元素,它按预期工作,在mutate_all
/ mutate_each
内传递时,不应该按照列中可用的观察结果复制此行为:
>> scales::dollar(c(1, 1e4))
[1] "$1" "$10,000"
答案 0 :(得分:4)
当我们在as.character
中使用package::function
时,我们需要用funs
打包,因为这似乎是一个错误。记录在案here
mtcars %>%
mutate_each(funs(as.character(scales::dollar(.))))
此外,函数mutate_each
已根据@Frank共享的博客链接弃用,因此我们可以使用mutate_at
mtcars %>%
mutate_at(names(.), funs(as.character(scales::dollar(.))))
补救措施是加载包,然后在没有::
library(scales)
mtcars %>%
mutate_at(names(.), funs(dollar(.)))
或者
mtcars %>%
mutate_at(names(.), dollar)