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

前端 未结 1 1452
慢半拍i
慢半拍i 2020-12-18 15:24

i have two data frames which are cleaned and merged as a single csv file , the data frames are like this

  **Source                         Master**

 chang         


        
相关标签:
1条回答
  • 2020-12-18 16:18

    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)
    
    0 讨论(0)
提交回复
热议问题