[R] String manipulation and formatting

Hans-Joerg Bibiko bibiko at eva.mpg.de
Tue Jul 18 10:18:17 CEST 2006


Hi,

an other way without any libraries and written as a one-line-command  
would be something like this:


xify <- function(x)
{
	gsub("[0-9]","X",
	sprintf(
		paste(
			"%",
			ifelse(
				format(x,digit=1)==as.character(x),
				paste(as.character(x),".0",sep=""),
				as.character(x)
			),
			"f",sep=""
		)
		,10^(trunc(x)-10*x+10*trunc(x)-1)
	)
	)
}


The only disadvantage is that something like xify(4.8) produces [1]  
"X.XXXXXXXX".

Hans-Joerg


>> On 7/17/06, Bashir Saghir (Aztek Global) <Saghir.Bashir at ucb- 
>> group.com> 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
>>>
>>>
>>> ---------------------------------------------------------
>>> Legal Notice: This electronic mail and its attachments are i... 
>>> {{dropped}}
>>>
>>> ______________________________________________
>>> R-help at 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
>>>
>>
>
> ______________________________________________
> R-help at 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
>
>



More information about the R-help mailing list