数字格式,写1e-5而不是0.00001

发布于 2024-09-28 10:50:50 字数 135 浏览 0 评论 0原文

我使用 read.table 读取包含数字(如 0.00001)的文件

,当我使用 write.table 写回这些数字时,这些数字显示为 1e-5

我怎样才能保留旧格式?

I've used read.table to read a file that contains numbers such as 0.00001

when I write them back with write.table those numbers appear as 1e-5

How can I keep the old format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱暴击 2024-10-05 10:50:50

我只需在调用 write.table 之前更改 scipen 选项即可。请注意,这也会更改打印到控制台时数字的显示方式。

options(scipen=10)
write.table(foo, "foo.txt")
options(scipen=0)  # restore the default

I would just change the scipen option before calling write.table. Note that this will also change how numbers are displayed when printing to the console.

options(scipen=10)
write.table(foo, "foo.txt")
options(scipen=0)  # restore the default
青瓷清茶倾城歌 2024-10-05 10:50:50

您可以通过根据需要将数字转换为具有格式的字符串,然后在调用 write.table 时使用参数 quote = FALSE 来实现此目的。

dfr <- data.frame(x = 10^(0:15))
dfr$y <- format(dfr$x, scientific = FALSE)
write.table(dfr, file = "test.txt", quote = FALSE)

请注意,您不需要更改文件中数字的格式。几乎每个科学软件和每个电子表格都可以理解数字的科学记数法,并且还具有数字格式选项,以便您可以按照自己的选择查看它们。

You can do this by converting your numbers to strings with formatting as you require, then using the argument quote = FALSE in the call to write.table.

dfr <- data.frame(x = 10^(0:15))
dfr$y <- format(dfr$x, scientific = FALSE)
write.table(dfr, file = "test.txt", quote = FALSE)

Note that you shouldn't need to change the format of the numbers in your file. Pretty much every piece of scientific software and every spreadsheet understands scientific notation for numbers, and also has number formatting options so you can view them how you choose.

自演自醉 2024-10-05 10:50:50

如果输入是科学记数法和显式记数法数字的混合,那么您将编写自己的解析器来读取数字并跟踪哪些数字采用哪种格式。事实上,您需要保留这些数字的字符串表示形式,以便您可以准确写回输入中的内容。

但是,如果您只想使用一致的显式符号来 write.table(),请尝试。

    write.table(format(_your_table_here_, scientific=FALSE), ...)

If the input is a mixture of scientific notation and explicit notation numbers, then you will be writing your own parser to read in the numbers and keep track of which ones were in which formats. In fact, you'll want to keep a string representation of those numbers lying around so you can write back exactly what was in the input.

However, if you just want to write.table() with consistently explicit notation, try.

    write.table(format(_your_table_here_, scientific=FALSE), ...)
趁微风不噪 2024-10-05 10:50:50

为了最大程度地控制所有行的循环并将它们打印到使用 sprintf 格式化的文本文件中

# Find number of rows in data.frame test
nrows <- dim(test)[1]

# init a new vector
mylines  <- vector("character",dim(test)[1])

# loop over all rows in dataframe
for(i in 1:nrows){
  # Print out exactly the format you want
  mylines[i] <- sprintf("Line %d: %.2f\t%.2f",1,test[i,"x"],test[i,"y")
}

# write lines to file
writeLines(mylines,"out.txt")

For maximum control loop over all rows and print them to a text file formatted with sprintf

# Find number of rows in data.frame test
nrows <- dim(test)[1]

# init a new vector
mylines  <- vector("character",dim(test)[1])

# loop over all rows in dataframe
for(i in 1:nrows){
  # Print out exactly the format you want
  mylines[i] <- sprintf("Line %d: %.2f\t%.2f",1,test[i,"x"],test[i,"y")
}

# write lines to file
writeLines(mylines,"out.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文