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}}
See ?formatC You might need to write a simple wrapper function to implement the interface that you want. -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Bashir Saghir (Aztek Global) Sent: Monday, July 17, 2006 10:07 AM To: 'r-help at R-project.org' Subject: [R] String manipulation and formatting 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
Thanks. I should have been clearer. The output is the string "XXX.XX" and "XXX". So xify(4.1) should produce "XXX.X" as character string. Any pointers? Or did I miss something with formatC? -----Original Message----- From: Christos Hatzis [mailto:christos@nuverabio.com] Sent: Monday, July 17, 2006 16:21 To: Bashir Saghir (Aztek Global); r-help@R-project.org Subject: RE: [R] String manipulation and formatting See ?formatC You might need to write a simple wrapper function to implement the interface that you want. -Christos -----Original Message----- From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Bashir Saghir (Aztek Global) Sent: Monday, July 17, 2006 10:07 AM To: 'r-help@R-project.org' Subject: [R] String manipulation and formatting 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 --------------------------------------------------------- Legal Notice: This electronic mail and its attachments are i...{{dropped}}
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=".") }
Thanks! I tried something similar but I had sep="" instead of collapse="" for the X part. I realise the error now. Thanks again Saghir -----Original Message----- From: Christos Hatzis [mailto:christos@nuverabio.com] Sent: Monday, July 17, 2006 16:39 To: Bashir Saghir (Aztek Global); r-help@R-project.org Subject: RE: [R] String manipulation and formatting Ok. You needed the format string. xify(4.1) would be equivalent to the following: paste(paste(rep("X",4),collapse=""),paste(rep("X",1),collapse=""),sep=".") It should be straightforward to write the wrapper function for this. HTH. -Christos _____ From: Bashir Saghir (Aztek Global) [mailto:Saghir.Bashir@UCB-Group.com] Sent: Monday, July 17, 2006 10:39 AM To: 'christos@nuverabio.com'; r-help@R-project.org Subject: RE: [R] String manipulation and formatting Thanks. I should have been clearer. The output is the string "XXX.XX" and "XXX". So xify(4.1) should produce "XXX.X" as character string. Any pointers? Or did I miss something with formatC? -----Original Message----- From: Christos Hatzis [mailto:christos@nuverabio.com <mailto:christos@nuverabio.com> ] Sent: Monday, July 17, 2006 16:21 To: Bashir Saghir (Aztek Global); r-help@R-project.org Subject: RE: [R] String manipulation and formatting See ?formatC You might need to write a simple wrapper function to implement the interface that you want. -Christos -----Original Message----- From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-bounces@stat.math.ethz.ch <mailto:r-help-bounces@stat.math.ethz.ch> ] On Behalf Of Bashir Saghir (Aztek Global) Sent: Monday, July 17, 2006 10:07 AM To: 'r-help@R-project.org' Subject: [R] String manipulation and formatting 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 <https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html <http://www.R-project.org/posting-guide.html> --------------------------------------------------------- Legal Notice: This electronic mail and its attachments are intended solely for the person(s) to whom they are addressed and contain information which is confidential or otherwise protected from disclosure, except for the purpose for which they are intended. Dissemination, distribution, or reproduction by anyone other than the intended recipients is prohibited and may be illegal. If you are not an intended recipient, please immediately inform the sender and return the electronic mail and its attachments and destroy any copies which may be in your possession. UCB screens electronic mails for viruses but does not warrant that this electronic mail is free of any viruses. UCB accepts no liability for any damage caused by any virus transmitted by this electronic mail. --------------------------------------------------------- --------------------------------------------------------- Legal Notice: This electronic mail and its attachments are intended solely for the person(s) to whom they are addressed and contain information which is confidential or otherwise protected from disclosure, except for the purpose for which they are intended. Dissemination, distribution, or reproduction by anyone other than the intended recipients is prohibited and may be illegal. If you are not an intended recipient, please immediately inform the sender and return the electronic mail and its attachments and destroy any copies which may be in your possession. UCB screens electronic mails for viruses but does not warrant that this electronic mail is free of any viruses. UCB accepts no liability for any damage caused by any virus transmitted by this electronic mail. --------------------------------------------------------- [[alternative HTML version deleted]]
THANKS! Exactly what I needed. Saghir -----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}}
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, > SaghirHere 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
First we define repx(n) to produce n X's. This simple function is used subsequently. f is a function that takes 4 strings which represent the entire string and the three backreferenced substrings in the subsequent gsub pattern and does the paste. x1 will represent the part before the dot and x3 after the dot. x2 represents the dot plus x3 and is not used. Finally we run gsubfn using the appropriate regex pattern. library(gsubfn) xify <- function(fmt) { repx <- function(n) paste(rep("X", n), collapse = "") f <- function(x, x1= "", x2 = "", x3 = "") if (x3 == "") repx(x1) else paste(repx(x1), ".", repx(x3), sep = "") as.vector(gsubfn("^([[:digit:]]+)(.([[:digit:]])|)", f, as.character(fmt))) } xify(5.2) # XXXXX.XX xify(5) # XXXXX 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 >
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}}
In my opinion there is nothing wrong with asking the user to enter strings as strings. I am working on some functions for SAS users (with limited R/S experience) who are used to working with the SAS macro language. I was trying to keep things simple for them by not forcing the use of quotes (a style they use with their SAS macros). Given your explanation I'll just document this feature in the help pages and use my time more productively else where. Thanks, Saghir -----Original Message----- From: Prof Brian Ripley [mailto:ripley@stats.ox.ac.uk] Sent: Tuesday, July 18, 2006 10:24 To: Bashir Saghir (Aztek Global) Cc: 'r-help@R-project.org' Subject: Re: [R] String manipulation and formatting On Tue, 18 Jul 2006, Bashir Saghir (Aztek Global) wrote:> 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 the10> without having to force the user to add quotes around the format?No, as the parser has converted '15.10' to a numeric constant. What is the problem with asking users to enter strings as strings? -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 --------------------------------------------------------- Legal Notice: This electronic mail and its attachments are i...{{dropped}}
As I am trying to maintain consistency with some SAS macros I prefer not to change the "standard definitions" in use at the moment. If I change things too much I don't think many people with use my new functions. Thanks, Saghir -----Original Message----- From: sean.oriordain@gmail.com [mailto:sean.oriordain@gmail.com] On Behalf Of Sean O'Riordain Sent: Tuesday, July 18, 2006 10:50 To: Bashir Saghir (Aztek Global) Cc: r-help@R-project.org Subject: Re: [R] String manipulation and formatting Does it have to be a stop char ".", or could it be a separate parameter, i.e. put in a comma, then the 10 becomes an integer... s/ On 18/07/06, Prof Brian Ripley <ripley@stats.ox.ac.uk> wrote:> On Tue, 18 Jul 2006, Bashir Saghir (Aztek Global) wrote: > > > 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 the10> > without having to force the user to add quotes around the format? > > No, as the parser has converted '15.10' to a numeric constant. > > What is the problem with asking users to enter strings as strings? > > -- > Brian D. Ripley, ripley@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > > ______________________________________________ > 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. >--------------------------------------------------------- Legal Notice: This electronic mail and its attachments are i...{{dropped}}