-1

I have a data-frame which has a column with strings like this Live by Night | Buy or rent on Blu-ray, DVD or digital. I want to keep the string only before the | symbol. so the sub-string result that I want from the above one is "Live by Night"
Thanks in advance for your valuable inputs.

www
  • 38,575
  • 12
  • 48
  • 84
Raj
  • 5
  • 2
  • 1
    https://stackoverflow.com/questions/15895050/using-gsub-to-extract-character-string-before-white-space-in-r or https://stackoverflow.com/questions/19320966/get-the-strings-before-the-comma-with-r or exactly https://stackoverflow.com/questions/38291794/extract-string-before A little research goes a long way. – thelatemail Jul 25 '17 at 04:42

1 Answers1

3

One option is sub to match zero or more space (\\s*) followed by | (metacharacter, so we need to escape to read it as the literal character, otherwise it is meant for OR) followed by other characters (.*) and replace it with blank ("")

sub("\\s*\\|.*", "", str1)
#[1] "Live by Night"

data

str1 <-  "Live by Night | Buy or rent on Blu-ray, DVD or digital"
akrun
  • 874,273
  • 37
  • 540
  • 662