Re: [R] String manipulation and formatting

From: Hans-Joerg Bibiko <bibiko_at_eva.mpg.de>
Date: Tue 18 Jul 2006 - 18:18:17 EST

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@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@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@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@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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Jul 18 18:23:38 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 - 20:17:00 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.