小编dkr*_*der的帖子

使用R中的format()格式化数字向量

当我尝试使用format()格式化数字向量时,只有第一个数字尊重"digits"参数

format(c(1.508390e-06, 8.487128e-02,  4.185008e-01,  4.785161e-01, -8.332557e-01),        
    digits = 3, scientific = FALSE)
[1] " 0.00000151" " 0.08487128" " 0.41850080" " 0.47851610" "-0.83325570"
Run Code Online (Sandbox Code Playgroud)

但是,如果我依次对每个元素进行透析,我会得到预期的结果.

sapply(c(1.508390e-06, 8.487128e-02,  4.185008e-01,  4.785161e-01, -8.332557e-01), FUN = function(x) { format(x,digits = 3, scientific = FALSE) } )
[1] "0.00000151" "0.0849"     "0.419"      "0.479"      "-0.833"    
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?

请注意,如果我设置scientific = FALSE,那么所有数字的格式都正确:

format(c(1.508390e-06, 8.487128e-02,  4.185008e-01,  4.785161e-01, -8.332557e-01),        
        digits = 3, scientific = TRUE)
" 1.51e-06" " 8.49e-02" " 4.19e-01" " 4.79e-01" "-8.33e-01"
Run Code Online (Sandbox Code Playgroud)

format formatting r

6
推荐指数
1
解决办法
103
查看次数

标签 统计

format ×1

formatting ×1

r ×1