简体   繁体   中英

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?

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("|"))

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM