Thanks to Richard, Gabor and Marc for some nice solutions to my request.

I have a new problem:

  > xify(30.10)
  [1] "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.X"
  > xify(30.11)
  [1] "XXXXXXXXXXXXXXXXXXX.XXXXXXXXXXX"

The problem originates from:

  > as.numeric(unlist(strsplit(as.character(15.10), "\\.")))
  [1] 15  1
  > as.numeric(unlist(strsplit(as.character(15.11), "\\.")))
  [1] 15 11

It seems to boils down to:

  > as.character(15.10)
  [1] "15.1"

A simple solution is:

  > xify("15.10")
  [1] "XXXXX.XXXXXXXXXX"

I was wondering if there is a simple way for xify to see the zero of the 10
without having to force the user to add quotes around the format?

Thanks,
Saghir


-----Original Message-----
From: Marc Schwartz (via MN) [mailto:mschwartz@mn.rr.com] 
Sent: Monday, July 17, 2006 16:55
To: Bashir Saghir (Aztek Global)
Cc: 'r-help@R-project.org'
Subject: Re: [R] String manipulation and formatting

<snip>

Here are two variations:

xify <- function(x)
{
  exxes <- as.numeric(unlist(strsplit(as.character(x), "\\.")))
  ifelse(length(exxes) == 2,
         paste(paste(rep("X", exxes[1] - exxes[2]), collapse = ""), 
               paste(rep("X", exxes[2]), collapse = ""), 
               sep = "."),
         paste(rep("X", exxes[1]), collapse = ""))
}


xify <- function(x)
{
  exxes <- as.numeric(unlist(strsplit(as.character(x), "\\.")))
  tmp <- sapply(exxes, function(x) paste(rep("X", x), collapse = ""))
  ifelse(length(tmp) == 2, 
         paste(substr(tmp[1], 1, exxes[1] - exxes[2]), tmp[2], 
               sep = "."),
         tmp)
}


HTH,

Marc Schwartz


-----Original Message-----
From: Richard M. Heiberger [mailto:rmh@temple.edu] 
Sent: Monday, July 17, 2006 16:42
To: Bashir Saghir (Aztek Global); 'r-help@R-project.org'
Subject: Re: [R] String manipulation and formatting


xify <- function(number) {
  fn <- format(number)
  fn3 <- unlist(strsplit(fn, "\\."))
  if (length(fn3) == 1) paste(rep("X", as.numeric(fn3)), collapse="")
  else
  paste(paste(rep("X", as.numeric(fn3)[1]-as.numeric(fn3)[2]), collapse=""),
        paste(rep("X", as.numeric(fn3)[2]), collapse=""),
        sep=".")
}


--------------------------------------------------------- 
Legal Notice: This electronic mail and its attachments are i...{{dropped}}

