Search code examples
rstringpattern-matchingstring-matchingsparse-dataframe

Grep and append column in R


I have 2000 lines of HR dataset and need to append column after grepping the string pattern. I want to match (sometimes they are not exact matches) edu column from df2 to df1, and print the respective Dep rows.

Also, when there is no match of edu pattern in df1, paste the same Dep string again instead of NA and vice versa (as in last 2 lines of expected result). Any suggestions? Thanks.

df2 <- data.frame(Dep = c("Research & Development", "Sales", "Research & Development", "Research & Development", "sales", "sales"), Edu = c("Life Sciences", "Marketing", "paramedical", "Other", "Technical studies","Business"))

df1 <- data.frame(Dep = c("Sales", "Sales", "Research & Development", "Research & Development", "Human Resources", "Research & Development", "legal section"), Edu = c("Life Sciences", "Marketing", "Medical", "Other", "Human Resources", "Technical", "Law"))

Expected output

        Dep_df1          Edu_df1_df2          Dep_df2
        Sales          Life Sciences          Research & Development 
        Sales          Marketing          Sales
        Research & Development          Medical          Research & Development 
        Research & Development          Other          Research & Development 
        Human Resources          Human Resources          Human Resources 
        Research & Development          Technical          Sales
        legal section          Law          legal section
        sales          Business           sales

Solution

  • After some tries, this worked.

    dd=merge(df1, df2[, c("Edu", "Dep")], by="Edu", all.x = TRUE) 
    transform(dd, dep.yfill = pmax(Dep.x, Dep.y, na.rm = TRUE))