Delete dot string in data frame

Hi community

Im want delete the point in ACCESION varaible. When I try with gsub this delete all names of this column.

ALE <-  structure(list(ACCESION = c("G10942.", "G10943.", "G10944A", 
  "G10950.", "G10953.", "G10957.", "G10959.", "G10962.", "G10965.", 
  "G10972."), HERBARIO = c("G10942.jpg", "G10943.jpg", "G10944A.jpg", 
    "G10950.jpg", "G10953.jpg", "G10957.jpg", "G10959.jpg", "G10962.jpg", 
    "G10965.jpg", "G10972.jpg")), row.names = c(NA, -10L), class = c("tbl_df", 
      "tbl", "data.frame"))

ALE$ACCESION <-  gsub(pattern = '.',x = ALE$ACCESION, replacement = '')

# A tibble: 10 × 2
# ACCESION HERBARIO   
# <chr>    <chr>      
#   1 ""       G10942.jpg 
# 2 ""       G10943.jpg 
# 3 ""       G10944A.jpg
# 4 ""       G10950.jpg 
# 5 ""       G10953.jpg 
# 6 ""       G10957.jpg 
# 7 ""       G10959.jpg 
# 8 ""       G10962.jpg 
# 9 ""       G10965.jpg 
# 10 ""       G10972.jpg

The idea is obtain this, without final string dot (.) in ACCESION

# A tibble: 208 × 2
   ACCESION HERBARIO   
     <chr>    <chr>      
# 1 G10942 G10942.jpg 
# 2 G10943  G10943.jpg 
# 3 G10944A  G10944A.jpg
# 4 G10950  G10950.jpg 
# 5 G10953  G10953.jpg 
# 6 G10957  G10957.jpg 
# 7 G10959  G10959.jpg 
# 8 G10962  G10962.jpg 
# 9 G10965  G10965.jpg 
# 10 G10972  G10972.jpg 

Tnks!

Im find this solution, but don't understand why with simple dot (.) not function.

ALE$ACCESION <-  gsub(pattern = '.[0−9]*$',x = ALE$ACCESION, replacement = '')

gsub uses Regular Expressions and . is a metacharacter that is interpreted as "any character", if you want to match a literall dot, you have to scape it \\.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.