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.ofans中间名和名字一起?或者他们自己的专栏?最好是名字。对不起,我对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