[Rd] sprintf

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Feb 15 12:54:47 CET 2005


sprintf() in R-devel is now vectorized, re-cycling its arguments, 
including fmt, as required.

On Tue, 15 Feb 2005, Wolfgang Huber wrote:

> Hi Peter,
>
> thanks. Yet my intention was not to discuss whether this could be done at all 
> (never had any doubts about that), but how it could be done nicely and 
> conveniently for the application programmer.
>
>  Best regards
>  Wolfgang
>
> Peter Dalgaard wrote:
>> Wolfgang Huber <huber at ebi.ac.uk> writes:
>> 
>> 
>>> Personally, I would prefer the recycling, but of course it can be also
>>> done this way:
>>> 
>>> > mapply(sprintf, "%04d %s", 1:3, "abc")
>>>    %04d %s       <NA>       <NA>
>>> "0001 abc" "0002 abc" "0003 abc"
>>> 
>>> the only slightly unaesthetic thing being the names of the resulting 
>>> vector.
>> 
>> 
>> ...which is of course fixable with either of
>> 
>> 
>> 
>>> mapply(sprintf, MoreArgs=list(fmt="%04d %s"), 1:3, "abc")
>> 
>> [1] "0001 abc" "0002 abc" "0003 abc"
>> 
>>> mapply(sprintf, 1:3, "abc", fmt="%04d %s")
>> 
>> [1] "0001 abc" "0002 abc" "0003 abc"
>> 
>>> mapply(sprintf, "%04d %s", 1:3, "abc", USE.NAMES=FALSE)
>> 
>> [1] "0001 abc" "0002 abc" "0003 abc"
>> 
>> (Only the last one is completely failsafe since the first two relies
>> on 1:3 not being character:
>> 
>>     if (USE.NAMES && length(dots) && is.character(dots[[1]]) &&
>>         is.null(names(answer)))
>>         names(answer) <- dots[[1]]
>> )
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list