将数字列表转换为字符时,如何控制数字的格式? [英] How do you control the formatting of digits when converting lists of numbers to character?

查看:42
本文介绍了将数字列表转换为字符时,如何控制数字的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个包含数字的嵌套列表

I have a nested list containing numbers, for example

x <- list(1 + .Machine$double.eps, list(rnorm(2), list(rnorm(1))))

如果我在此上调用 as.character ,则所有数字均以固定格式给出,最多15位有效数字.

If I call as.character on this, all the numbers are given in fixed format, to 15 significant digits.

as.character(x)
## [1] "1"                                                                      
## [2] "list(c(0.654345721043012, 0.611306113713901), list(-0.278722330674071))"

我希望能够控制数字的格式.至少,我希望能够控制包含多少个有效数字.另外,能够指定科学格式而不是固定格式会很好.

I'd like to be able to control how the numbers are formatted. At the very least, I'd like to be able to control how many significant figures are included. As a bonus, being able to specify scientific formatting rather than fixed formatting would be nice.

as.character 帮助页面状态:

as.character表示实数和复数,有效位数为15数字(从技术上讲,是ISO C常数的编译器设置DBL_DIG,在支持IEC60559算法的机器上为15根据C99标准).这样可以确保输入的所有数字结果将是可靠的(而不是表示的结果)错误),但这确实意味着要转换为字符然后再转换为数字可能会更改号码.如果要将数字转换为字符尽可能以最高的精度使用格式.

as.character represents real and complex numbers to 15 significant digits (technically the compiler's setting of the ISO C constant DBL_DIG, which will be 15 on machines supporting IEC60559 arithmetic according to the C99 standard). This ensures that all the digits in the result will be reliable (and not the result of representation error), but does mean that conversion to character and back to numeric may change the number. If you want to convert numbers to character with the maximum possible precision, use format.

因此似乎无法直接使用 as.character 更改格式.

So it doesn't appear to be possible to change the formatting using as.character directly.

调用 format 会破坏列表结构:

format(x, digits = 5)
## [1] "1"                          "0.65435, 0.61131, -0.27872"

formatC 引发有关不支持列表输入的错误.

formatC throws an error about not supporting list inputs.

deparse 也不允许用户更改数字的格式设置: as.character(x) vapply(x,deparse,字符(1)).

deparse also doesn't allow users to change how numbers are formatted: as.character(x) is the same as vapply(x, deparse, character(1)).

这几乎是正确的,但是在我不需要的数字周围还有多余的双引号字符:

This is almost correct, but there are extra double-quote characters around the numbers that I don't want:

as.character(rapply(x, format, digits = 5, how = "list"))
## [1] "1"
## [2] "list(c(\"0.65435\", \"0.61131\"), list(\"-0.27872\"))"

如何控制数字的格式?

部分解决方案:为了减少有效数字的数量,我可以通过使用格式转换为字符然后再转换为数字的方式来适应上一个示例.

A partial solution: for reducing the number of significant figures, I can adapt the previous example by converting to character using format, then back to numeric.

as.character(rapply(x, function(x) as.numeric(format(x, digits = 5)), how = "list"))
## [1] "1"                                       "list(c(-1.0884, 1.6892), list(0.58783))"

如果我想将无花果的数量增加到15个以上或使用科学格式,这是行不通的(因为我们遇到了 as.character 的限制).

This doesn't work if I want to increase the number of sig figs beyond 15 or use scientific formatting (since we run into the limitation of as.character).

as.character(rapply(x, function(x) as.numeric(format(x, digits = 22)), how = "list"))
## [1] "1"                                                                  
## [2] "list(c(-1.08842504028146, 1.68923191896784), list(0.5878275490431))"

推荐答案

播放 rapply() how 参数:

> rapply(x, sprintf, fmt = "%0.5f", how = "replace")
[[1]]
[1] "1.00000"

[[2]]
[[2]][[1]]
[1] "0.18041"  "-0.63925"

[[2]][[2]]
[[2]][[2]][[1]]
[1] "0.14309"

要获取更多数字,请更改 fmt :

For more digits, change fmt:

> rapply(x, sprintf, fmt = "%0.22f", how = "replace")
[[1]]
[1] "1.0000000000000002220446"

[[2]]
[[2]][[1]]
[1] "1.2888001496908956244880" "1.0289289081633956612905"

[[2]][[2]]
[[2]][[2]][[1]]
[1] "0.4656598705611921240610"

您可以 gsub()排除引号:

> gsub("\"", "", deparse(rapply(x, function(z) sprintf(fmt = "%0.22f", z), how = "replace")))
[1] "list(1.0000000000000002220446, list(c(1.2888001496908956244880, "
[2] "1.0289289081633956612905), list(0.4656598705611921240610)))"

这篇关于将数字列表转换为字符时,如何控制数字的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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