Removing leading numbers in text string

  • Removing leading numbers in text string

     clint.thomson updated 4 weeks ago 2Members · 3 Posts
  • clint.thomson

    Member
    December 22, 2020 at 12:40 pm

    Hello!

    I have the following string: “1. The resources were helpful (e.g. computers, office supplies).”

    I am hoping to modify it to: “The resources were helpful (e.g. computers, office supplies).”

    How can I identify a number followed by a period and a space as the text I want to remove? I’m currently looking into the str_remove, str_replace, and gsub functions.

    Any help is greatly appreciated. Thanks!

    • This discussion was modified 4 weeks, 1 day ago by  clint.thomson.
  • David

    Organizer
    December 22, 2020 at 2:20 pm

    I can think of two ways to do this.

    The best way is probably with what’s known as regular expressions. Think of this as find and replace on steroids. You should be able to write something with str_remove() and a regular expression to remove the numbers. I’m not good at all with regular expressions, but I am working with someone to develop a course in 2021 that will cover it. You also might check out the RVerbalExpressions package.

    I wrote some less elegant, but effective code that does this (see gist here). It uses the separate() function to break apart the text into two variables (this is after converting it into a tibble) and then keeps only the part after the .

    Hope that helps!

  • clint.thomson

    Member
    December 22, 2020 at 5:40 pm

    Works great! Thanks, David. I hadn’t used the extra argument in the separate function before. Basically, it looks like writing: exact = “merge”, will prevent the periods in “e.g.” from being identified as separators? This is part of what was creating my initial issue. Take care.

Viewing 1 - 3 of 3 posts

Log in to reply.

Original Post
0 of 0 posts June 2018
Now