Re: [R] format negative numbers

2014-10-22 Thread PIKAL Petr
ginal Message- > From: David Winsemius [mailto:dwinsem...@comcast.net] > Sent: Monday, October 20, 2014 11:04 PM > To: PIKAL Petr > Cc: Marc Girondot; r-help@r-project.org > Subject: Re: [R] format negative numbers > > > On Oct 20, 2014, at 4:32 AM, PIKAL Petr wrote: > &g

Re: [R] format negative numbers

2014-10-20 Thread David Winsemius
; already invented clever way how to deal with such numbers. > > Cheers > Petr > > >> -Original Message- >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- >> project.org] On Behalf Of Marc Girondot >> Sent: Monday, October 20, 2014 12:5

Re: [R] format negative numbers

2014-10-20 Thread MacQueen, Don
You could do it with minimal use of regular expressions, along the lines of this example: x <- c('123','2.31','2.313-', '45-') is.neg <- grepl('-',x) xn <- x xn[is.neg] <- paste0( '-', substring(x[is.neg],1, nchar(x[is.neg])-1)) xn <- as.numeric(xn) I made a copy, 'xn', so that

Re: [R] format negative numbers

2014-10-20 Thread PIKAL Petr
d clever way how to deal with such numbers. Cheers Petr > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Marc Girondot > Sent: Monday, October 20, 2014 12:53 PM > To: r-help@r-project.org > Subject: Re: [R] form

Re: [R] format negative numbers

2014-10-20 Thread Marc Girondot
Is it what you want? > st <- "0.123-" > gsub("(.+)(-)", "\\2\\1", st) [1] "-0.123" > st <- "0.123" > gsub("(.+)(-)", "\\2\\1", st) [1] "0.123" Sincerely Marc Le 20/10/2014 09:03, PIKAL Petr a écrit : Dear all. Before I start fishing in (for me) murky regular expression waters I try to ask co

Re: [R] format negative numbers

2014-10-20 Thread Rainer Schuermann
Maybe not the most elegant way but at least works: library( stringr ) x <- as.factor( "123.4-" ) x <- -as.numeric( str_replace( as.character( x ), "-", "" ) ) x [1] -123.4 On Monday 20 October 2014 09:03:36 PIKAL Petr wrote: > Dear all. > > Before I start fishing in (for me) murky regular expr