shridhi pandya shridhi pandya - 3 months ago 13
R Question

Match two columns in r of different csv and print the matching keyword in the column with respected category




I've two different csv files File 1 with Keywordscolumn and File 2 with Keywords and category column. I'm looking forward to match the keyword file (File 1) with the keyword column in File 2 and if it matches it should print the category(column in File2) of the keyword matched.
eg:

File1

#Keywords
HDFC
Insurance
Patym
BookMyShow


File2

#Keywords(Column)
SBI Bank
Insurance
Paytm
BookMyShow


#Category(Column)
Bank
Insurance
Wallet
Entertainment


what i'm looking forward is to finding out a solution to match the Keywords column of different csv and if the keyword matches it should print it along with the respected category.

Answer

Is this what you want?

KeywordsFile1 <- c("HDFC", "Insurance", "Patym", "BookMyShow")
KeywordsFile2 <- c("SBI Band", "Insurance", "Patym", "BookMyShow")
Category <- c("Bank", "Insurance", "Wallet", "Entertainment")

Category[KeywordsFile1 %in% KeywordsFile2]
[1] "Insurance"     "Wallet"        "Entertainment"

EDIT: To obtain also keyword you can use this code

key.match <- KeywordsFile1 %in% KeywordsFile2
cbind(KeywordsFile1[key.match], Category[key.match])