R sprintf函数

------------恢复内容开始------------
看到sprintf函数有点不明所以,原文如下

为了用指定的格式数值型转换成字符型, 可以使用sprintf()函数, 其用法与C语言的sprintf()函数相似, 只不过是向量化的。例如
sprintf('file%03d.txt', c(1, 99, 100))
## [1] "file001.txt" "file099.txt" "file100.txt"

sprintf()

Use C-style String Formatting Commands

Description

A wrapper for the C function sprintf, that returns a character vector containing a formatted combination of text and variable values.

Usage

sprintf(fmt, ...)
gettextf(fmt, ..., domain = NULL)
Arguments
fmt 格式化字符串
a character vector of format strings, each of up to 8192 bytes.

...
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. Up to 100.

domain
see gettext.

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.

gettextf is a convenience function which provides C-style string formatting with possible translation of the format string.

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.

The following is abstracted from Kernighan and Ritchie (see References): however the actual implementation will follow the C99 standard and fine details (especially the behaviour under user error) may depend on the platform.
------------恢复内容结束------------

上一篇:sprintf_s() 、sprintf()和printf()区别和用法


下一篇:DL4NLP —— 序列标注:BiLSTM-CRF模型做基于字的中文命名实体识别