[R] chage the output file name

Duncan Murdoch murdoch at stats.uwo.ca
Wed Sep 23 02:55:55 CEST 2009


On 22/09/2009 8:19 PM, Rolf Turner wrote:
> On 23/09/2009, at 12:14 PM, Henrique Dallazuanna wrote:
> 
>> Try this:
>>
>> treeName <- "PINE"
>> write.table(d, file = sprintf("C:/%sheight.txt", treeName))
> 
> Why not use the (less esoteric) paste() function?

The printf() family of functions are one of the good things of the C 
language.  I'm with Henrique here, it makes the code easier to read than 
the error-prone paste().

There's one bad thing in C:  the lack of type checking, so that you can 
use formats that make no sense given the args.  The R implementation (by 
Brian Ripley) adds type checking, so for example you would get a "too 
few arguments" error if you tried

sprintf("C:/%sheight.txt")

(Well, maybe the strange syntax of the formats in C isn't perfect, but 
it's still easier to read than

paste("C:/", treeName, "height.txt")

and the error in the above is pretty hard to spot.)

Duncan Murdoch




More information about the R-help mailing list