You need to trim the last line of characters only if it is empty or "."

I have a large vector of words read from an excel file. Some of these entries end with a space or ".". period. Only in those cases I need to trim these characters.

Example:

"depresion"                              "tristeza."                             
"nostalgia"                              "preocupacion."                         
"enojo."                                 "soledad "                              
"frustracion"                            "desesperacion "                        
"angustia."                              "desconocidos."                         

Note that some words end normally without a "." or "".

Is there any way to do this?

I have it

substr(conceptos, 1, nchar(conceptos)-1)) 

to check the last character (conceptos is a long vector)

Thanks for any advice.

+4
source share
3 answers

We can use subto match zero or more .or spaces and replace it with empty ( "")

sub("(\\.| )*$", "", v1)
#[1] "depresion"     "tristeza"      "nostalgia"     "preocupacion"  "enojo"   
#[6] "soledad"       "frustracion"   "desesperacion"
#[9] "angustia"      "desconocidos" 

data

v1 <- c("depresion","tristeza.","nostalgia","preocupacion.",
   "enojo.","soledad ","frustracion","desesperacion ",
   "angustia.","desconocidos.")
+4
source

Regular expressions are suitable for this:

library(stringr)

x = c("depresion", "tristeza.", "nostalgia", "preocupacion.", 
      "enojo.", "soledad ", "frustracion", "desesperacion ", 
      "angustia.", "desconocidos.")
x_replaced = str_replace(x, "(\\.|\\s)$", "")

(\\.|\\s)$ . , .

+4

:

iif ((middle (cropping (concept), Len (concept), 1) == ".")? substr (conceptos, 1, nchar (conceptos) -1)): trim (conceptos))

-3
source

Source: https://habr.com/ru/post/1686005/


All Articles