From p.dalgaard at biostat.ku.dk  Mon Feb 14 13:35:31 2005
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Mon Feb 14 13:40:28 2005
Subject: [Rd] sprintf - was formatC with illegal input crashes Rgui
	(PR#7686)
In-Reply-To: <42107994.9070500@ebi.ac.uk>
References: <20050211175824.5EB48DEE7@slim.kubism.ku.dk>
	<Pine.LNX.4.61.0502140956020.29523@gannet.stats>
	<4210741A.8040606@ebi.ac.uk>
	<Pine.LNX.4.61.0502141052360.30303@gannet.stats>
	<42107994.9070500@ebi.ac.uk>
Message-ID: <x2mzu7jp4c.fsf@biostat.ku.dk>

Wolfgang Huber <huber@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]]
)
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907


[Rd] sprintf - was formatC with illegal input crashes Rgui (PR#7686)
Peter Dalgaard