Find Match of two data frames and rewrite the answer as data frame

混江龙づ霸主 提交于 2019-11-29 08:50:17

If you want to check the Master.Names only against the first word in Names, this could do the trick:

Names$Mast <- NA
for(i in seq_len(nrow(Names))) 
    Names$Mast[i] <- grep(toupper(x = strsplit(Names[i,1]," ")[[1]][1]), Master.Names$V1,value=TRUE)

Edit

Using sapply instead of a loop could gain you some speed:

Names$Mast <- sapply(Names$V1, function(x) {
    grep(toupper(x = strsplit(x," ")[[1]][1]), Master.Names$V1,value=TRUE)
})

Results

> Names
                        V1                            Mast
1 chang chun petrochemical                CHANG CHUN GROUP
2      chang chun plastics                CHANG CHUN GROUP
3            church dwight        CHURCH AND DWIGHT CO INC
4   citrix systems pacific CITRIX SYSTEMS ASIA PACIFIC P L

Data

Master.Names <- read.csv(text="CHANG CHUN GROUP
CHURCH AND DWIGHT CO INC
CITRIX SYSTEMS ASIA PACIFIC P L
CNH INDUSTRIAL N.V", header=FALSE)

Names <- read.csv(text="chang chun petrochemical
chang chun plastics     
church dwight          
citrix systems pacific", header=FALSE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!