Removing single character in the end of each sentence

Hi,
I have this simple data file:

URN <- c("fff", "ggg", "hhh", "iii", "jjj", "kkk", "nnn")
Status <- c("/", "New/Old/", "New/", "New/ Old/", "/", "Old/", "New/")
My.data <- data.frame(URN, Status)
My.data

Can you please help me to remove "/" as standalone character or in the end of each Status statement (record with New and Old should stay unchanged)?

You can do it with regular expressions

library(stringr)

Status <- c("/", "New/Old/", "New/", "New/ Old/", "/", "Old/", "New/")

str_remove(Status, "/$")
#> [1] ""         "New/Old"  "New"      "New/ Old" ""         "Old"     
#> [7] "New"

Note: Please stop using the #rstudio-ide category for this kind of questions, that category is for problems with the Integrated Development Environment

1 Like

Sorry for silly question but I cannot find a solution to do it in the beginning, as a single character and in the end...

library(stringr)

Status <- c("/", "/New/Old/", "New/", "New/ Old/", "/", "Old/", "New/")

str_remove(Status, "$/")

as the above does not remove anything from the beginning :thinking:

$ is for end, and ^ is for start.

Just adding a note that @andresrcs has used it in a lot of your previous threads. Please understand when you get a solution, and don't just copy paste.

1 Like

Most of your follow up questions are not about stringr or any tidyverse package but about "regular expressions", so basically you keep asking us for writing regex for you, don't you think it would be better if you take the time to learn regex yourself?

Yes, you're right. That was silly. I promise not to ask silly questions anymore. Thank you for your patience!!!

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