[R] format negative numbers

Rainer Schuermann rainer.schuermann at gmx.net
Mon Oct 20 12:48:31 CEST 2014


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 expression waters I try to ask community about changing format of negative numbers.
> 
> For some reason I get a file with negative numbers formatted with negative sign at end of number.
> 
> something like
> 
> 0.123-
> 
> It is imported as factors and I need to convert it to numbers again. Before converting I need to change it to "correct" format
> 
> -0.123
> 
> Does anybody know some simple way?
> 
> Cheers
> Petr
.
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list