找回密码
 注册
查看: 6376|回复: 0

R语言:formatc()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 17:24:11 | 显示全部楼层 |阅读模式
formatc(base)
formatc()所属R语言包:base

                                        Formatting Using C-style Formats
                                         使用C-风格的格式的格式

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Formatting numbers individually and flexibly, using C style format specifications.
格式化数字独立和灵活,使用C风格的格式规范。


用法----------Usage----------


formatC(x, digits = NULL, width = NULL,
        format = NULL, flag = "", mode = NULL,
        big.mark = "", big.interval = 3L,
        small.mark = "", small.interval = 5L,
        decimal.mark = ".", preserve.width = "individual",
        zero.print = NULL, drop0trailing = FALSE)

prettyNum(x, big.mark = "",   big.interval = 3L,
          small.mark = "", small.interval = 5L,
          decimal.mark = ".",
          preserve.width = c("common", "individual", "none"),
          zero.print = NULL, drop0trailing = FALSE, is.cmplx = NA,
          ...)



参数----------Arguments----------

参数:x
an atomic numerical or character object, possibly complex only for prettyNum(), typically a vector of real numbers.
一个原子的数字或字符的对象,可能只对complexprettyNum(),通常是实数的向量。


参数:digits
the desired number of digits after the decimal point (format = "f") or significant digits (format = "g", = "e" or = "fg").  Default: 2 for integer, 4 for real numbers.  If less than 0, the C default of 6 digits is used.  If specified as more than 50, 50 will be used with a warning unless format = "f" where it is limited to typically 324. (Not more than 15–21 digits need be accurate, depending on the OS and compiler used.  This limit is just a precaution against segfaults in the underlying C runtime.)  
小数点后所需的位数(format = "f")或有效位数(format = "g",= "e"或= "fg")。默认:实数为整数,4 2。如果小于0,C,其默认的6位数。如果超过50,50将使用一个警告,除非format = "f"它通常仅限于324。 (不超过15-21个数字是准确的,这取决于操作系统和编译器使用,这个限制仅仅是一个预防措施,对在底层的C运行时出现segfaults)。


参数:width
the total field width; if both digits and width are unspecified, width defaults to 1, otherwise to digits + 1.  width = 0 will use width = digits, width < 0 means left justify the number in this field (equivalent to flag ="-"). If necessary, the result will have more characters than width.  For character data this is interpreted in characters (not bytes nor display width).  
总场宽度;如果两个digits和width是不确定的,width默认为1,否则digits + 1。 width = 0将使用width = digits,width < 0的意思是离开的理由在这一领域的数量(相当于flag ="-")。如果有必要,其结果将有更多的字符比width。这是字符数据字符(而不是字节也显示宽度)的解释。


参数:format
equal to "d"  (for integers), "f", "e", "E", "g", "G", "fg" (for reals), or "s" (for strings).  Default is "d" for integers, "g" for reals.  "f" gives numbers in the usual xxx.xxx format;  "e" and "E" give n.ddde+nn or n.dddE+nn (scientific format); "g" and "G" put x[i] into scientific format only if it saves space to do so.  "fg" uses fixed format as "f", but digits as the minimum number of significant digits.  This can lead to quite long result strings, see examples below.  Note that unlike signif this prints large numbers with more significant digits than digits.  Trailing zeros are dropped in this format, unless flag contains "#".
等于"d"(整数),"f","e","E","g","G","fg"(实数的),或"s"(字符串)。默认是"d"整数,"g"雷亚尔。 "f"给出通常xxx.xxx格式数字;"e"和"E"给n.ddde+nn或n.dddE+nn(科学);"g"和"G"把x[i]成科学格式,只有当它节省空间,这样做的。 "fg"使用"f"的固定格式,但digits有效位数的最低数量。这可能会导致相当长的结果字符串,见下面的例子。请注意,与signif这比digits更有效位数的大量打印。尾随零被丢弃在此格式,除非flag包含"#"的。


参数:flag
For formatC, a character string giving a format modifier as in Kernighan and Ritchie (1988, page 243). "0"  pads leading zeros; "-" does left adjustment, others are "+", " ", and "#".  There can be more than one of these, in any order.  
formatC,Kernighan和Ritchie在给字符串的格式修饰符(1988年,第243页)。 "0"垫前导零;"-"不离开的调整,其他都是"+"" ","#"。可以有超过其中之一,在任何命令。


参数:mode
"double" (or "real"), "integer" or "character". Default: Determined from the storage mode of x.
"double"(或"real")"integer"或"character"。默认:从x的存储模式的决心。


参数:big.mark
character; if not empty used as mark between every big.interval decimals before (hence big) the decimal point.
字符;如果不是空的,因为每big.interval小数之间的商标使用前(因此big)小数点。


参数:big.interval
see big.mark above; defaults to 3.
看到big.mark以上;默认为3。


参数:small.mark
character; if not empty used as mark between every small.interval decimals after (hence small) the decimal point.
字符;如果不为空(因此small.interval)小数点后每small小数之间的商标。


参数:small.interval
see small.mark above; defaults to 5.
看到small.mark以上;默认为5。


参数:decimal.mark
the character to be used to indicate the numeric decimal point.
字符被用来表示数字的小数点。


参数:preserve.width
string specifying if the string widths should be preserved where possible in those cases where marks (big.mark or small.mark) are added.  "common", the default, corresponds to format-like behavior whereas "individual" is the default in formatC().
字符串,指定如果字符串宽度应保留在这种情况下可能引号(big.mark或small.mark)添加。 "common",默认情况下,对应的format般的行为,而"individual"是默认formatC()。


参数:zero.print
logical, character string or NULL specifying if and how zeros should be formatted specially.  Useful for pretty printing "sparse" objects.
逻辑,字符串或NULL指定是否以及如何零,特别应格式化。漂亮的印刷“疏”的对象非常有用。


参数:drop0trailing
logical, indicating if trailing zeros, i.e., "0" after the decimal mark, should be removed; also drops "e+00" in exponential formats.
逻辑表示,如果尾随零,即"0"小数点后的商标,应当拆除;也下降"e+00"在指数格式。


参数:is.cmplx
optional logical, to be used when x is "character" to indicate if it stems from complex vector or not.  By default (NA), x is checked to "look like" complex.
可选的逻辑,时要使用x是"character"表明,如果它源于complex向量或不。默认情况下(NA)x检查“看起来像”复杂。


参数:...
arguments passed to format.
参数传递到format。


Details

详情----------Details----------

If you set format it overrides the setting of mode, so formatC(123.45, mode="double", format="d") gives 123.
如果你设置了format覆盖mode的设置,所以formatC(123.45, mode="double", format="d")给123。

The rendering of scientific format is platform-dependent: some systems use n.ddde+nnn or n.dddenn rather than n.ddde+nn.
科学格式的渲染是依赖于平台:有些系统使用n.ddde+nnn或n.dddenn而不是n.ddde+nn。

formatC does not necessarily align the numbers on the decimal point, so formatC(c(6.11, 13.1), digits=2, format="fg") gives c("6.1", " 13").  If you want common formatting for several numbers, use format.
formatC并不一定对齐小数点的数字,所以formatC(c(6.11, 13.1), digits=2, format="fg")给c("6.1", " 13")。如果你想几个号码常见的格式,使用format。

prettyNum is the utility function for prettifying x. x can be complex (or format(<complex>), here.  If x is not a character, format(x[i], ...) is applied to each element, and then it is left unchanged if all the other arguments are at their defaults.  Note that prettyNum(x) may behave unexpectedly if x is a character vector not resulting from something like format(<number>): in particular it assumes that a period is a decimal mark.
prettyNum美化x的实用功能。 x可以是复杂的(或format(<complex>),在这里。x如果是不是一个字符,format(x[i], ...)被应用到每个元素,然后保持不变,如果所有其他参数是在它们的默认值注意prettyNum(x)行为可能意外x如果character向量不产生类似format(<number>):尤其是它假定一个时期是1小数大关。

Because gsub is used to insert the big.mark and small.mark, special characters need escaping.  In particular,  to insert a single backslash, use "\\\\".  
因为gsub是用来插入big.mark和small.mark,特殊字符需要转义。特别是,要插入一个反斜杠,使用"\\\\"。

In versions of R before 2.13.0, the big.mark would be reversed on insertion if it contained more than one character.  
在2.13.0之前的版本的R,big.mark将在插入逆转,如果它包含多个字符。


值----------Value----------

A character object of same size and attributes as x, in the current locale's encoding. Unlike format, each number is formatted individually. Looping over each element of x, the C function sprintf(...) is called for numeric inputs (inside the C function str_signif).
一个相同的大小和属性x字符对象,在当前的区域设置的编码。不像format,每个数字单独进行格式化。在每个元素循环x,C函数sprintf(...)被称为数字输入(内部C函数str_signif)。

formatC: for character x, do simple (left or right) padding with white space.
formatC:字符x,做简单的白色空间(左或右)填充。


作者(S)----------Author(s)----------



<code>formatC</code> was originally written by Bill Dunlap, later much
improved by Martin Maechler.
It was first adapted for <font face="Courier New,Courier" color="#666666"><b>R</b></font> by Friedrich Leisch.




参考文献----------References----------

The C Programming Language.  Second edition.  Prentice Hall.

参见----------See Also----------

format.
format。

sprintf for more general C like formatting.
sprintf一般般&#231;格式。


举例----------Examples----------


xx  <- pi * 10^(-5:4)
cbind(format(xx, digits=4), formatC(xx))
cbind(formatC(xx, width = 9, flag = "-"))
cbind(formatC(xx, digits = 5, width = 8, format = "f", flag = "0"))
cbind(format(xx, digits=4), formatC(xx, digits = 4, format = "fg"))

formatC(    c("a", "Abc", "no way"), width = -7)  # &lt;=&gt; flag = "-"[<=>标志“ - ”]
formatC(c((-1:1)/0,c(1,100)*pi), width=8, digits=1)

xx <- c(1e-12,-3.98765e-10,1.45645e-69,1e-70,pi*1e37,3.44e4)
##       1        2             3        4      5       6[#1 2 3 4 5 6]
formatC(xx)
formatC(xx, format="fg")       # special "fixed" format.[特殊的“固定”格式。]
formatC(xx[1:4], format="f", digits=75) #&gt;&gt; even longer strings[>>甚至更长的字符串]

formatC(c(3.24, 2.3e-6), format="f", digits=11, drop0trailing=TRUE)

r <- c("76491283764.97430", "29.12345678901", "-7.1234", "-100.1","1123")
## American:[#美国:]
prettyNum(r, big.mark = ",")
## Some Europeans:[#一些欧洲人:]
prettyNum(r, big.mark = "'", decimal.mark = ",")

(dd <- sapply(1:10, function(i)paste((9:0)[1:i],collapse="")))
prettyNum(dd, big.mark="'")

## examples of 'small.mark'[“small.mark#范例]
pN <- stats::pnorm(1:7, lower.tail = FALSE)
cbind(format (pN, small.mark = " ", digits = 15))
cbind(formatC(pN, small.mark = " ", digits = 17, format = "f"))

cbind(ff <- format(1.2345 + 10^(0:5), width = 11, big.mark = "'"))
## all with same width (one more than the specified minimum)[#所有相同的宽度(小于规定的最小一)]

## individual formatting to common width:[#个别格式化共同宽度:]
fc <- formatC(1.234 + 10^(0:8), format="fg", width=11, big.mark = "'")
cbind(fc)

## complex numbers:[#复数:]
r <- 10.0000001; rv <- (r/10)^(1:10)
(zv <- (rv + 1i*rv))
op &lt;- options(digits=7) ## (system default)[#(系统默认)]
(pnv <- prettyNum(zv))
stopifnot(pnv == "1+1i", pnv == format(zv),
          pnv == prettyNum(zv, drop0trailing=TRUE))
## more digits change the picture:[#更多的数字更改图片:]
options(digits=8)
head(fv <- format(zv), 3)
prettyNum(fv)
prettyNum(fv, drop0trailing=TRUE) # a bit nicer[一点更好]
options(op)

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-22 21:34 , Processed in 0.028314 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表