dimitris_ps dimitris_ps - 3 months ago 9
R Question

Remove comma and or period except if certain condition holds for last occurrence in R

I would like to remove all commas and periods from string, except in the case that a string ends in a comma (or period) followed by one or two numbers.

Some examples would be:

12.345.67 #would become 12345.67
12.345,67 #would become 12345,67
12.345,6 #would become 12345,6
12.345.6 #would become 12345.6
12.345 #would become 12345
1,2.345 #would become 12345


and so forth

Answer

Another option is to use negative look ahead syntax ?! with the perl compatible regex:

df
#          V1
# 1 12.345.67
# 2 12.345,67
# 3  12.345,6
# 4  12.345.6
# 5    12.345
# 6   1,2.345

df$V1 = gsub("[,.](?!\\d{1,2}$)", "", df$V1, perl = T)
df          # remove , or . except they are followed by 1 or 2 digits at the end of string
#         V1
# 1 12345.67
# 2 12345,67
# 3  12345,6
# 4  12345.6
# 5    12345
# 6    12345
Comments