lolo
lolo

Reputation: 646

replace data frame - special pattern

Suppose I have the next data frame:

dd<-data.frame(a=c("xtr","la casa x-tr","x-tr"))

             a
          xtr
 la casa x-tr
         x-tr

How can I replace onlye the "x-tr" occurrencies with "xtr". So, final output would be

         a
          xtr
 la casa xtr
         xtr

Upvotes: 0

Views: 21

Answers (1)

akrun
akrun

Reputation: 887118

We can use sub

dd$a <- sub("(x)-(tr)$", "\\1\\2", dd$a)
dd$a
#[1] "xtr"         "la casa xtr" "xtr"  

If there is only a single -, then

sub("-", "", dd$a)

Upvotes: 1

Related Questions