Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用ifelse命令进行字符串解析_Regex_R_Parsing - Fatal编程技术网

Regex 使用ifelse命令进行字符串解析

Regex 使用ifelse命令进行字符串解析,regex,r,parsing,Regex,R,Parsing,我在将一系列名字解析为名字和姓氏时遇到问题。给出如下所示的名称列表: names <- as.vector(c("Rep. Mike Grimm","House Committee on Energy and Commerce", "Office of the Inspector General","Rep. J. Gresham Barrett","Rep. Mary Fallin")) name这就是你的想法吗?我不会将对象命名为“names”,因为

我在将一系列名字解析为名字和姓氏时遇到问题。给出如下所示的名称列表:

names <- as.vector(c("Rep. Mike Grimm","House Committee on Energy and Commerce",
                 "Office of the Inspector General","Rep. J. Gresham Barrett","Rep. Mary Fallin"))

name这就是你的想法吗?我不会将对象命名为“names”,因为您将屏蔽函数“names”


name.of
ans中间名和名字一起?或者他们自己的专栏?最好是名字。对不起,我对regex非常陌生,正在尝试弄清楚这一切……理想情况下,我想在ifelse命令中使用它,这样我就可以将不包含“Rep”的办公室标记为NA。有没有办法将名字提取到一列中,将姓氏提取到另一列中?
names.of <- as.vector(c("Rep. Mike Grimm","House Committee on Energy and Commerce",
                  "Office of the Inspector General","Rep. J. Gresham Barrett","Rep. Mary Fallin"))
names.rep<-grep("Rep",names.of,value=T )
gsub("Rep\\. ([A-Za-z]+)","\\1",names.rep)
ans <- gsub('Rep. ', '', names[grep('Rep. ', names)])
First <- gsub('\\s\\w+$', '', ans)
Last <- gsub('.*?(\\w+$)', '\\1', ans)
df <- data.frame(First, Last)
df
#       First    Last
#1       Mike   Grimm
#2 J. Gresham Barrett
#3       Mary  Fallin