0

I am trying to remove all text after "-" including the "-" such as "Albany-Schenectady, Allentown-Bethlehem" etc.

I tried using Gsub, but having trouble getting the code to work.

#What I tried to make work
gsub("(.*)-.*", "\\1",  
Koolakuf_DR
  • 467
  • 4
  • 16
  • 1
    Do you have multiple in `"-"` in a text string? – Mike H. Mar 29 '18 at 18:49
  • Try `"([^-]*)-.*"` – R. Schifini Mar 29 '18 at 18:50
  • related: https://stackoverflow.com/questions/13093931/r-remove-last-word-from-string – Artem Sokolov Mar 29 '18 at 18:51
  • The problem with your method is the greediness of the first `.*` when there are multiple hyphens. `gsub("(.*)-.*", "\\1", "Albany-Schenectady, Allentown-Bethlehem")` works, but it grabs until the last hyphen. R Schifini's recommendation avoids that, and seeellayewhy's answer is a simpler approach that also fixes the problem. – Gregor Thomas Mar 29 '18 at 18:57

1 Answers1

7

That's an incomplete line of code and not really even on the right track for what you've described. This should work.

gsub("-.*", "", vector)

The first argument tells it to grab the hyphen and everything after it to be replaced by the second argument, an empty string. The third argument is the vector you're performing the operation on.

cparmstrong
  • 799
  • 6
  • 23