![](/img/trans.png)
[英]r keeping only specific digits after the decimal point and printing the number
[英]R options(digits=2) function changes the total number of digits’ format. Looking for a way to change the digits after decimal point
options(digits=2)
改變了前進的總位數:
> options(digits=3)
> 1.234
[1] 1.23
> 12.234
[1] 12.2
> 123.234
[1] 123
有沒有辦法改變數字格式,以便更改小數點后的數字,所以使用digits_after_decimal = 3,數字1.12345變為1.123,123.12345變為123.123?
我知道圓形或格式的功能; 但是,它們只是根據參數返回結果,而選項(數字= 3)會改變所有格式。
在R中請求改進的“報告”選項有很長的歷史,但從未遇到過對base-R的修改。 不可能改變print.default
函數的行為,這是你正在使用的(不知道如此)通過翻轉選項開關進行舍入。 (您可以通過使用methods
檢查沒有print.numeric
。)所以我的建議是學習在要舍入的項目上使用round
, sprintf
或formatC
。 將會有一個額外的“障礙”,sprintf或formatC結果將是一個字符值,除非你使用cat
打印,否則它將顯示引號,或者為print
調用設置quote=FALSE
。 @digEmAll根據他的建議說明了其中的兩個問題。 您可能希望查看更多“完整”策略,以獲取各種“表”函數和包提供的報告格式。 您可以通過使用“table”和“tabular”搜索可用包來查看各種各樣的內容。 我經驗豐富的包括htmlTables
, tabular
和xtable
但還有很多其他的,這不包括knitr
和Sweave
選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.