简体   繁体   English

更改R中的默认数字格式

[英]Change default number formatting in R

Is there a way to change the default number formatting in R so that numbers will print a certain way without repeatedly having to use the format() function? 有没有一种方法可以更改R中的默认数字格式,以便数字将以某种方式打印而不必重复使用format()函数? For example, I would like to have 例如,我想要

> x <- 100000
> x
[1] 100,000

instead of 代替

> x <- 100000
> x
[1] 100000

Well if you want to save keystrokes, binding the relevant R function to some pre-defined key-strokes, is fast and simple in any of the popular text editors. 好吧,如果您想保存击键,将相关的R函数绑定到一些预定义的击键,则在任何流行的文本编辑器中都是快速而简单的。

Aside from that, I suppose you can always just write a small formatting function to wrap your expression in; 除此之外,我想您总是可以编写一个小的格式化函数来包装您的表达式。 so for instance: 因此例如:

fnx = function(x){print(formatC(x, format="d", big.mark=","), quote=F)}

> 567 * 43245
[1] 24519915

> fnx(567*4325)
[1] 2,452,275

R has several utility functions that will do this. R有几个实用程序函数可以做到这一点。 I prefer "formatC" because it's a little more flexible than 'format' and 'prettyNum'. 我更喜欢“ formatC”,因为它比“ format”和“ prettyNum”更具灵活性。

In my function above, i wrapped the formatC call in a call to 'print' in order to remove the quotes (") from the output, which i don't like (i prefer to look at 100,000 rather than "100,000" ). 在上面的函数中,我将formatC调用包装在对“打印”的调用中,以便从输出中删除引号(“)(我不喜欢(我更喜欢看100,000而不是” 100,000“ ))。

I don't know how to change the default (in fact, I would advise against it because including the comma makes it a character). 我不知道如何更改默认值(实际上,我建议不要这样做,因为包括逗号会使它成为字符)。

You can do this: 你可以这样做:

> prettyNum(100000, big.mark=",", scientific=FALSE)
[1] "100,000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM