Re: [R] String manipulation and formatting

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Tue 18 Jul 2006 - 00:55:08 EST

On Mon, 2006-07-17 at 16:07 +0200, Bashir Saghir (Aztek Global) wrote:
> I'm trying to write a simple function that does the following:
>
> [command] xify(5.2)
> [output] XXX.XX
>
> [command] xify(3)
> [output] XXX
>
> Any simple solutions (without using python/perl/unix script/...)?
>
> Thanks,
> Saghir

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 18 01:02:14 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 18 Jul 2006 - 02:19:44 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.