I have two datasets called A and B.
library(data.table)
Farm.Type <- c("Fruits","Vegetables","Livestock")
Produce.All <- c("Apple, Orange, Pears, Strawberries","Broccoli, Cabbage, Spinach","Cow, Pig, Chicken")
Store <- c("Convenience","Wholesale","Grocery","Market")
Produce <- c("Oranges","Watermelon","Cabbage","Pig")
Farm <- c("Fruits","","Vegetables","Livestock")
A <- data.table(Farm.Type, Produce.All)
B <- data.table(Store, Produce)
I am trying to identify what Farm.Type the Produce in table B falls into in table A, without changing the format of the two tables in order to pull the Farm.Type field into table B. Such that the data frame looks like
C <- data.table(Store, Produce, Farm)
I have tried using %in% in the following way:
B$Farm[B$Produce %in% A$Produce.All] <- A$Farm.Type
but because the A$Produce.All field is a string with commas, It does not match.
Is there a way to search through the string (A$Produce.All) to find the match for B$Produce?
Any help is appreciated.
Thanks.