Check if value from one dataframe exists in another

I am a beginner in R and want to know how it will works for each row.


I have a two data frames.

I want to see if the rows exist in the other multiple columns of other data frame.
I want to loop every row in ab$Fruits and check if each name is somewhere in another dataframe bc[E1:E3]columns.

    ab<-data.frame(Fruits = c("Apple","Banana"),Units = c("3","2"))



I want to do with loops or any function.When the loops starts, first row of ab$fruits match with each rows of bc,if first row (Apple) match with apple in bc$E1 ,in ab$new column the result should return 1 and loop breaks.Again 2nd row (Banana) search for each column in bc,if it is not match with any row in column bc$E1 ,in ab$new column result should return 0.[I want to do this only to make sum of ab$units == 5]Till it makes sum=5 loop should iterates.

When 3rd time ab$fruits(apple) loop reach from row 1 (Apple) and match with column bc$E3 also row 2 (Banana) matches with Banana the result should be ab$new = 1 for both the rows.

IF ab$new for both the rows ==1 and sum of both the units is >=5 ,create new column in ab$view = "Bill"
output ab:

SN Fruits    Number   New**  view
1     Apple      3      1   Bill
2    Banana    2      1    Bill  

I have tried %in% but not happening.

Need advice.

I'm afraid your mechanical description of how to process the two dataframes is quite confusing. Perhaps you can try explaining what you wish to achieve by providing a context. i.e. what does the first frame represent. a stock request ? available stock?
what does the second frame represent ? rows are orders, each collumn is what is ordered. and you are trying to relate the information how and for what purpose ?
I'm hopeful that if we understood why your data is structured how it is, and what you want to use it for, we can help you write the code to process it.

If I understand your problem you want to see if the fruits in ab exist in bc$E1:E3? If that is the case then the function below should solve your problem.

purrr::map_df(bc, ~ .x %in% ab$Fruits)

A tibble: 4 x 3

E1 E2 E3


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.