[英]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 |
我们可以使用
fixed
作为|
in default mode in regex is a metacharacter suggesting OR
. 在regex的默认模式下,是一个元字符,建议
OR
。 So, if we want to get the literal value, use fixed
or escape ( \\\\
) or place it inside square brackets 因此,如果要获取文字值,请使用
fixed
或转义( \\\\
)或将其放在方括号内
library(stringr)
str_extract(mycharacter, fixed("|"))
You can use gsub
: 您可以使用
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 然后,您可以使用构建一个
data.frame
myDF <- data.frame(Directors = director, Stars= star)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.