0

I have a dataframe as follows:

         A      B
 mediafile      1
 filemedia      1
media time      1
time media      1

How do I remove the word "media" only if it is the last string in the column. Final Output:

         A      B
 mediafile      1
      file      1
media time      1
     time       1

Thanks!

1 Answer 1

2

In regex, $ means "end of the string", so media$ will match media only if it is immediately followed by the end of the string.

Use gsub for find/replace:

your_data$A = gsub(pattern = "media$", replacement = "", x = your_data$A)

R uses regex the same as any other language, so in the future I'd recommend searching SO for something like "[regex] at end of string", which turned up this question, from which you probably could have generalized.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.