1

So I have a data frame that includes a column like this: image

And I would like to remove the operator as well as the numbers to the right of it, i.e. so the first entry would just say 51.81 rather than 51.81 - 11.19. How would I go about this? I feel like using a for loop might work but I'm unsure of the syntax required.

Thanks

Conor Neilson
  • 1,026
  • 1
  • 11
  • 27
toastienf
  • 57
  • 4
  • Please do not post screenshots - we cant copy paste from them. Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) using `dput` – Conor Neilson Mar 26 '20 at 18:34

1 Answers1

1

We can use sub to match zero or more spaces (\\s*) followed by a - or + and other characters, and replace with blank ("")

df1$xG <- as.numeric(sub("\\s*[-+]+.*", "", df1$xG))
akrun
  • 874,273
  • 37
  • 540
  • 662