Skip to content
Prev 96230 / 398500 Next

String manipulation and formatting

On Mon, 2006-07-17 at 16:07 +0200, Bashir Saghir (Aztek Global) wrote:
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