也許你可以做這方面的一些調整:
ifelse(x < 100, sprintf("%0.2f", x), sprintf("%0.5e", x))
# [,1] [,2] [,3] [,4] [,5]
#[1,] "9.99999e+06" "-0.79" "-0.56" "0.91" "-2.57"
#[2,] "-0.13" "9.99999e+06" "-1.83" "-0.34" "1.73"
#[3,] "-0.48" "0.38" "1.00000e+07" "1.40" "-0.32"
#[4,] "-0.05" "-0.62" "0.91" "1.00000e+07" "1.15"
#[5,] "-0.09" "-0.33" "-0.16" "0.35" "9.99999e+06"
或者不帶引號:
noquote(ifelse(x < 100, sprintf("%0.2f", x), sprintf("%0.5e", x)))
# [,1] [,2] [,3] [,4] [,5]
#[1,] 9.99999e+06 -0.79 -0.56 0.91 -2.57
#[2,] -0.13 9.99999e+06 -1.83 -0.34 1.73
#[3,] -0.48 0.38 1.00000e+07 1.40 -0.32
#[4,] -0.05 -0.62 0.91 1.00000e+07 1.15
#[5,] -0.09 -0.33 -0.16 0.35 9.99999e+06
你基本上是在轉換你想要的方式文本打印。有關輸出選項的更多信息,請參閱?sprintf
。