我能强迫R使用常规数字而不是类似e+10的符号吗?我有:

1.810032e+09
# and 
4

在相同的向量中,并希望看到:

1810032000
# and
4

我正在为一个老式的程序创建输出,我必须使用cat编写一个文本文件。 到目前为止还好,但我不能在这里使用e+10符号。


当前回答

在. rprofile文件中放置options(scipen = 999),这样默认情况下它会自动执行。(不要依赖于手动操作。)

(这与其他答案不同:如何?

This keeps things sane when you thunk between multiple projects, multiple languages on a daily or monthly basis. Remembering to type in your per-project settings is error-prone and not scalable. You can have a global ~/.Rprofile or per-project .Rprofile. Or both, with the latter overriding the former. Keeping all your config in a project-wide or global .Rprofile auto-executes it. This is useful for e.g. default package loads, data.table configuration, environment etc. Again, that config can run to a page of settings, and there's zero chance you'll remember those and their syntax and type them in

其他回答

在. rprofile文件中放置options(scipen = 999),这样默认情况下它会自动执行。(不要依赖于手动操作。)

(这与其他答案不同:如何?

This keeps things sane when you thunk between multiple projects, multiple languages on a daily or monthly basis. Remembering to type in your per-project settings is error-prone and not scalable. You can have a global ~/.Rprofile or per-project .Rprofile. Or both, with the latter overriding the former. Keeping all your config in a project-wide or global .Rprofile auto-executes it. This is useful for e.g. default package loads, data.table configuration, environment etc. Again, that config can run to a page of settings, and there's zero chance you'll remember those and their syntax and type them in

我最喜欢的答案是:

format(1810032000, scientific = FALSE)
# [1] "1810032000"

这就提供了你想要的,而不必在R设置中瞎折腾。

注意,它返回一个字符串而不是一个数字对象

它可以通过在R中禁用科学计数法来实现。

options(scipen = 999)

这是一个灰色地带。您需要回想一下,R总是会调用一个打印方法,而这些打印方法侦听一些选项。包括“scipen”——对科学展示的惩罚。从帮助(选项):

“scipen”:整数。当决定打印时要使用的惩罚 固定或指数表示法的数值。积极的 价值观偏向固定,对科学持否定态度 符号:除非有更多的固定符号,否则优先使用固定符号 比scipen的手指宽。

例子:

R> ran2 <- c(1.810032e+09, 4) 
R> options("scipen"=-100, "digits"=4)
R> ran2
[1] 1.81e+09 4.00e+00
R> options("scipen"=100, "digits"=4)
R> ran2
[1] 1810032000          4

尽管如此,我还是觉得它很软糖。最直接的方法是使用具有显式宽度的sprintf(),例如sprintf("%. exe ")。5 f”,ran2)。