user4381526
user4381526

Reputation:

Extract "|" from a character in R

This is my character vector:

mycharacter<-"    Directors:Chris Renaud, Yarrow Cheney                 |     Stars:Louis C.K., Eric Stonestreet, Kevin Hart, Lake Bell    "

Why I cant extract the "|" from my character?

Also, after extract "|" how can I build a data frame with two columns. One being Directors and other being Stars?

Any help?

Upvotes: 3

Views: 63

Answers (2)

fra
fra

Reputation: 852

You can use gsub:

 # return the left side of |
 gsub("^(.*)\\|(.*)$","\\1",mycharacter)
 [1] "    Directors:Chris Renaud, Yarrow Cheney                 "

 # return the right side of |
 gsub("^(.*)\\|(.*)$","\\2",mycharacter)
 [1] "     Stars:Louis C.K., Eric Stonestreet, Kevin Hart, Lake Bell    "

If you want to remove the spaces you can act on the regular expression (.*).

director <- gsub("^\\s+(.*)\\|(.*)$","\\1",mycharacter)
director <- gsub("\\s+$","",director)

star <- gsub("^(.*)\\|\\s+(.*)$","\\2",mycharacter)
star <- gsub("\\s+$","",star)

You can then build a data.frame with

 myDF <- data.frame(Directors = director, Stars= star) 

Upvotes: 2

akrun
akrun

Reputation: 887291

We can use fixed as the | in default mode in regex is a metacharacter suggesting OR. So, if we want to get the literal value, use fixed or escape (\\) or place it inside square brackets

library(stringr)
str_extract(mycharacter, fixed("|"))

Upvotes: 3

Related Questions