Is it possible to remove a given conditional character in R? [duplicate]

0

I need help to clean a database in R.

I need to remove a point that is at the beginning of some lines, I'm using R and the formula sub("^.","", x) the problem is that using this formula eliminates the 1st line of my entire base, Is it possible to use a conditional?

example: Original database

.2199.00
 99,90
 18,013
.123.00'

What I need

 2199.00
 99,90
 18,013
 123.00'

using sub("^.","", x)

 2199.00
 9,90
 8,013
 23.00'
    
asked by Guillermo Pardo Arteaga 08.05.2017 в 19:41
source

1 answer

2

In regular expressions the point is a reserved character (ie, it means more than the point as a character). To determine the point you must escape the character with "\" . See the example.


x <- c(".2199.00", "99,90", "18,013", ".123.00")
x
#> [1] ".2199.00" "99,90"    "18,013"   ".123.00"

gsub("^.", "", x)
#> [1] "2199.00" "9,90"    "8,013"   "123.00"

gsub(".", "", x)
#> [1] "" "" "" ""

gsub("^\.", "", x)
#> [1] "2199.00" "99,90"   "18,013"  "123.00"

In particular "." matchea any character, so in the second example it replaces everything.

    
answered by 08.05.2017 / 20:27
source