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

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

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

                                        Use C-style String Formatting Commands
                                         使用C风格的字符串格式化命令

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

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

A wrapper for the C function sprintf, that returns a character vector containing a formatted combination of text and variable values.
C函数sprintf包装,返回一个特征向量包含格式化文本和变量值的组合。


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


sprintf(fmt, ...)
gettextf(fmt, ..., domain = NULL)



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

参数:fmt
a character vector of format strings, each of up to 8192 bytes.
一个格式字符串的字符向量,每个可达8192字节。


参数:...
values to be passed into fmt.  Only logical, integer, real and character vectors are supported, but some coercion will be done: see the "Details" section.
值将被传递到fmt。只有逻辑,整数,实数和特征向量的支持,但一些强制将做到:见“详细资料”部分。


参数:domain
see gettext.
看到gettext。


Details

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

sprintf is a wrapper for the system sprintf C-library function.  Attempts are made to check that the mode of the values passed match the format supplied, and R's special values (NA, Inf, -Inf and NaN) are handled correctly.
sprintf是一个系统sprintfC库函数的包装。尝试检查值的模式,通过匹配的格式提供,R的特殊值(NA,Inf,-Inf和NaN)正确处理。

gettextf is a convenience function which provides C-style string formatting with possible translation of the format string.
gettextf是一个方便的功能,提供可能翻译的格式字符串与C风格的字符串格式化。

The arguments (including fmt) are recycled if possible a whole number of times to the length of the longest, and then the formatting is done in parallel.  Zero-length arguments are allowed and will give a zero-length result.  All arguments are evaluated even if unused, and hence some types (e.g., "symbol" or "language", see typeof) are not allowed.
参数(包括fmt)如果可能的整数次回收到的最长的长度,然后格式化并行完成。允许零长度的参数,并会提供一个零长度的结果。所有参数进行评估,即使不使用,因此某些类型(例如,"symbol"或"language",看到typeof)是不允许的。

The following is abstracted from Kernighan and Ritchie (see References).  The string fmt contains normal characters, which are passed through to the output string, and also conversion specifications which operate on the arguments provided through ....  The allowed conversion specifications start with a % and end with one of the letters in the set aAdifeEgGosxX%.  These letters denote the following types:
以下是抽象Kernighan和Ritchie(见参考资料)。字符串fmt包含正常的字符,这是通过对输出字符串,操作对...提供的参数的转换规格。允许的转换规格开始%和结束的字母集aAdifeEgGosxX%。这些字母表示以下几种类型:




d, i, o, x, X Integer value, o being octal, x and X being hexadecimal (using the same case for a-f as the code).  Numeric variables with exactly integer values will be coerced to integer.  Formats d and i can also be used for logical variables, which will be converted to 0, 1 or NA.
d, i, o, x, Xo是八进制,Integer值,x和X十六进制(使用a-f代码相同的情况下)。数字准确的整数值的变量将被强制转换为整数。格式d和i也可以用于逻辑变量,将转换到0,1或NA。




f Double precision value, in “fixed point” decimal notation of the form "[-]mmm.ddd".  The number of decimal places ("d") is specified by the precision: the default is 6; a precision of 0 suppresses the decimal point.  Non-finite values are converted to NA, NaN or (perhaps a sign followed by) Inf.
f双精度值,在“定点”的形式的十进制表示法“[ - ] mmm.ddd”。指定精度的小数位数(“D”):默认为6 0精密抑制小数点。非限定值被转换为NA,NaN(也许后跟一个标志)Inf。




e, E Double precision value, in “exponential” decimal notation of the form [-]m.ddde[+-]xx or [-]m.dddE[+-]xx.
e, E双精度值,在“指数”的十进制记数法的形式[-]m.ddde[+-]xx或[-]m.dddE[+-]xx。




g, G Double precision value, in %e or %E format if the exponent is less than -4 or greater than or equal to the precision, and %f format otherwise. (The precision (default 6) specifies the number of significant digits here, whereas in %f, %e, it is the number of digits after the decimal point.)
g, G双精度值,在%e或%E格式,如果指数小于-4或大于或等于精度,和%f格式,否则。 (指定若干重大数字的精度(默认为6)在这里,而在%f, %e,它是小数点后的位数。)




a, A Double precision value, in binary notation of the form [-]0xh.hhhp[+-]d.  This is a binary fraction expressed in hex multiplied by a (decimal) power of 2.  The number of hex digits after the decimal point is specified by the precision: the default is enough digits to represent exactly the internal binary representation.  Non-finite values are converted to NA, NaN or (perhaps a sign followed by) Inf.  Format %a uses lower-case for x, p and the hex values: format %A uses upper-case.
a, A双精度值的二进制表示形式[-]0xh.hhhp[+-]d。这是一个二进制数,由2(十进制)功率乘以十六进制表示。精度指定小数点后的十六进制数字:默认的是足够的数字来表示完全的内部二进制表示。非限定值被转换为NA,NaN(也许后跟一个标志)Inf。格式%ax,p“的十六进制值使用小写格式%A使用大写。

This should be supported on all platforms as it is a feature of C99. The format is not uniquely defined: although it would be possible to make the leading h always zero or one, this is not always done.  Most systems will suppress trailing zeros, but a few do not.  On a well-written platform, for normal numbers there will be a leading one before the decimal point plus (by default) 13 hexadecimal digits, hence 53 bits.  The treatment of denormalized (aka "subnormal") numbers is very platform-dependent.
这应当在所有平台上支持,因为它是一个功能的C99。格式是不是唯一的定义:虽然有可能使领先的h始终是零或一,这是不是一直在做。大多数系统将抑制尾随零,但几个不。一个写得很好的平台,为正常数会有小数点加前的领先者之一(默认)13个十六进制数字,因此53位。治疗非规范化(又名“低于正常”)号码是非常依赖于平台的。




s Character string.  Character NAs are converted to "NA".
s字符串。字符NA"NA"s的转换。




% Literal % (none of the extra formatting characters given below are permitted in this case).
%文学%(下面给出额外的格式字符没有在这种情况下允许)。

Conversion by as.character is used for non-character arguments with s and by as.double for non-double arguments with f, e, E, g, G.  NB: the length is determined before conversion, so do not rely on the internal coercion if this would change the length.  The coercion is done only once, so if length(fmt) > 1 then all elements must expect the same types of arguments.
as.character转换用于非字符参数与s和as.doublef, e, E, g, G的非双参数。注:长度决定转换前,所以不依靠内部的胁迫,这是否会改变长度。强制只进行一次,所以如果length(fmt) > 1然后所有元素都必须指望同一类型的参数。

In addition, between the initial % and the terminating conversion character there may be, in any order:
此外,与最初的%和终止转换字符有可能以任何顺序:




m.n Two numbers separated by a period, denoting the
m.n句点分隔的两个数字,表示




- Left adjustment of converted argument in its field.
-左调整在该领域的转换参数。




+ Always print number with sign: by default only
仅+始终打印带符号数:默认




a space Prefix a space if the first character is not a sign.
空间前缀的空间,如果第一个字符是不是一个迹象。




0 For numbers, pad to the field width with leading zeros.
0对于数字,垫带前导零的字段宽度。




# specifies “alternate output” for numbers, its action depending on the type: For x or X, 0x or 0X will be prefixed to a non-zero result.  For e, e, f, g and G, the output will always have a decimal point; for g and G, trailing zeros will not be removed.
#指定号码“备用输出”,它的作用取决于类型:x或X,0x或0X将到的前缀非零的结果。 e,e,f,g和G,输出将始终有一个小数点;g和<X >,尾随零将不会被删除。

Further, immediately after % may come 1$ to 99$ to refer to numbered argument: this allows arguments to be referenced out of order and is mainly intended for translators of error messages.  If this is done it is best if all formats are numbered: if not the unnumbered ones process the arguments in order. See the examples.  This notation allows arguments to be used more than once, in which case they must be used as the same type (integer, double or character).
此外,立即后%可能会1$99$参考编号的说法:这允许参数被引用的顺序,主要用于翻译错误消息。如果这样做,这是最好的,如果所有的格式编号:如果没有一间没有门牌的处理秩序的论据。见的例子。这个符号允许参数要使用一次以上,在这种情况下,他们必须为同一类型(整数,双或字符)。

A field width or precision (but not both) may be indicated by an asterisk *: in this case an argument specifies the desired number.  A negative field width is taken as a '-' flag followed by a positive field width.  A negative precision is treated as if the precision were omitted.  The argument should be integer, but a double argument will be coerced to integer.
字段的宽度或精度(但不是两者兼有)可能会以星号表示*:在这种情况下,一个参数指定所需的号码。负字段宽度是作为一个 - 标志跟着一个积极的字段宽度。负的精度的精度视为被省略。参数应该是整数,但双参数将被强制转换为整数。

There is a limit of 8192 bytes on elements of fmt, and on strings included from a single %letter conversion specification.
有一个8192字节的限制fmt元素,包括从单一%信转换规范的字符串。

Field widths and precisions of %s conversions are interpreted as bytes, not characters, as described in the C standard.
场宽度和精度的%s转换为字节,而不是在C标准中所述的字符,解释。


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

A character vector of length that of the longest input.  If any element of fmt or any character argument is declared as UTF-8, the element of the result will be in UTF-8 and have the encoding declared as UTF-8.  Otherwise it will be in the current locale's encoding.
特征向量的长度最长的输入。如果声明为UTF-8fmt或任何字符参数中的任何元素,元素的结果将是在UTF-8编码为UTF-8宣布。否则,它会在当前语言环境的编码。


警告----------Warning----------

The format string is passed down the OS's sprintf function, and incorrect formats can cause the latter to crash the R process .  R does perform sanity checks on the format, and since R 2.10.0, we have not seen crashes anymore.  But not all possible user errors on all platforms have been tested, and some might be terminal.
格式字符串是通过操作系统的sprintf功能,不正确的格式,可以导致后者崩溃的R过程。 &#345;格式进行健康检查,并自R 2.10.0,我们还没有看到崩溃了。但是,并非所有平台上的所有可能的用户错误已经过测试,以及一些可能是终端。


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



Original code by Jonathan Rougier.




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

The C Programming Language. Second edition, Prentice Hall. describes the format options in table B-1 in the Appendix.

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

formatC for a way of formatting vectors of numbers in a similar fashion.
formatC以类似的方式格式化的数字向量的一种方式。

paste for another way of creating a vector combining text and values.
paste创建一个向量相结合的文本和值的另一种方式。

gettext for the mechanisms for the automated translation of text.
gettext文本自动翻译的机制。


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


## be careful with the format: most things in R are floats[#注意格式:R中的大多数事情都是花车]
## only integer-valued reals get coerced to integer.[#只有整数值雷亚尔获得强制转换为整数。]

sprintf("%s is %f feet tall\n", "Sven", 7.1)      # OK[确定]
try(sprintf("%s is %i feet tall\n", "Sven", 7.1)) # not OK[不OK]
    sprintf("%s is %i feet tall\n", "Sven", 7  )  # OK[确定]

## use a literal % :[#使用文字%:]

sprintf("%.0f%% said yes (out of a sample of size %.0f)", 66.666, 3)

## various formats of pi :[PI#各种格式:]

sprintf("%f", pi)
sprintf("%.3f", pi)
sprintf("%1.0f", pi)
sprintf("%5.1f", pi)
sprintf("%05.1f", pi)
sprintf("%+f", pi)
sprintf("% f", pi)
sprintf("%-10f", pi) # left justified[左对齐]
sprintf("%e", pi)
sprintf("%E", pi)
sprintf("%g", pi)
sprintf("%g",   1e6 * pi) # -&gt; exponential[ - >指数]
sprintf("%.9g", 1e6 * pi) # -&gt; "fixed"[ - >“固定”]
sprintf("%G", 1e-6 * pi)

## no truncation:[#没有截断:]
sprintf("%1.f",101)

## re-use one argument three times, show difference between %x and %X[#重新使用一个参数的三倍,显示%X和%X之间的差异]
xx <- sprintf("%1$d %1$x %1$X", 0:15)
xx <- matrix(xx, dimnames=list(rep("", 16), "%d%x%X"))
noquote(format(xx, justify="right"))

## More sophisticated:[#更复杂的:]

sprintf("min 10-char string '%10s'",
        c("a", "ABC", "and an even longer one"))

n <- 1:18
sprintf(paste("e with %2d digits = %.",n,"g",sep=""), n, exp(1))

## Using arguments out of order[#使用参数顺序]
sprintf("second %2$1.0f, first %1$5.2f, third %3$1.0f", pi, 2, 3)

## Using asterisk for width or precision[#使用宽度或精度的星号]
sprintf("precision %.*f, width '%*.3f'", 3, pi, 8, pi)

## Asterisk and argument re-use, 'e' example reiterated:[#Asterisk和参数重新使用,e的例子重申:]
sprintf("e with %1$2d digits = %2$.*1$g", n, exp(1))

## re-cycle arguments[#周期参数]
sprintf("%s %d", "test", 1:3)

## binary output showing rounding/representation errors[#二进制输出显示四舍五入/表示错误]
x <- seq(0, 1.0, 0.1); y <- c(0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1)
cbind(x, sprintf("%a", x), sprintf("%a", y))

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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-24 05:02 , Processed in 0.052430 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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