在整个R会话中完全删除科学计数法 [英] Completely remove scientific notation for the entire R session

查看:164
本文介绍了在整个R会话中完全删除科学计数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不想在我的R会话的任何计算结果中都希望看到任何科学计数法.我想查看所有实际数字,最好每三位数后有一个逗号(,).

I do not want to see any scientific notation in any result of any calculation in my R session. I want to see all the actual numbers, preferably with a comma (,) after every three digits.

我该怎么做? options(scipen = 999)不会剪切它.

How can I do that? options(scipen = 999) does not cut it.

推荐答案

print.default函数在决定"要分配的宽度时查看options()['digits']值,因此您可能需要增加它并尝试最大输出"scipen".该宽度存在大于16的特定于操作系统的问题.至于逗号请求,请...将其忘记. R不是财务报告系统.如果需要强制使用此输出格式,则可以将对象定义为特定的类,并使用sprintf和formatC为其编写打印方法,或者我想可以重写print.default,但这可能只会影响不属于该类的打印操作传递给print的其他100多种方法之一.

The print.default function looks at the options()['digits'] value in "deciding" what width to allocate, so you may need to increase it as well as trying to max out 'scipen'. There are OS specific issues with values above 16 for that width. As for the commas request, ... forget it. R is not a financial reporting system. If you need to force this output format, you can define objects to be of a particular class and write print methods for them using sprintf and formatC, Or I suppose you can rewrite print.default, but that might only affect printing operations that were not passed to one of the other 100+ methods for print.

有输出方法. formatC()prettyNum()都有一个"big.mark"参数,该参数会将逗号插入数字中.输出为"character"格式,因此尝试对创建的结果进行任何进一步的计算.

There are output methods. formatC() and prettyNum() both have a 'big.mark' argument that will insert commas into numbers. The output is in "character" format, so do not try to do any further calculations on the results you create.

还有一些输入法可以读取包含逗号或货币符号的数字的列:

There are also input methods that could read columns with numbers containing commas or currency symbols:

setAs("character", "num.with.commas",  function(from) as.numeric(gsub(",", "", from))) 
setAs("character", "euro",
 function(from) as.numeric(gsub("€", "", from)))
setAs("character", "num_pct",
 function(from) as.numeric(gsub("%", "", from))/100)
# you will get warning messages if you have not defined the class,
#     ....  but this will still succeed

 Input <- "A B C
 1,000 1% 3.50€
 2,000 2% 4.77€
 3,000 3% €5.68
"
 DF <- read.table(textConnection(Input), header = TRUE,  
                colClasses = c("num.with.commas", "num_pct", "euro"))
 str(DF)

这篇关于在整个R会话中完全删除科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆