[R] using sprintf with dates

Alberto Monteiro albmont at centroin.com.br
Tue Oct 2 15:52:45 CEST 2007


stephen bond wrote:
> 
> foot=function(){
>   str1=format(Sys.Date,"%Y%m%d")
>   sprintf("99%-4s%s","nm",str1)
> }
> 
> I wanted to have "99nm  20071002" as the output.
> 
Sys.Date is a function. It's perfectly possible
to write string <- format(Sys.Date, "%s") (or, generically,
string <- format(sin, "%s"), etc), but it will just put some
description of the function in the string.

Also, the assignment in R is "<-", not "=".

Try:

foot <- function(){
  str1 <- format(Sys.Date(),"%Y%m%d")
  sprintf("99%-4s%s","nm",str1)
}

Alberto Monteiro



More information about the R-help mailing list