在dplyr中使用mutate_all格式化所有列

时间:2017-03-31 16:10:20

标签: r string formatting dplyr

我正在考虑使用 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"
    

1 个答案:

答案 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)