Cardinal
Cardinal

Reputation: 33

Using %in% to identify if an element is in a df

Here is my sample code:

DF$Owner <- ifelse (ExcelData$Brand %in% Reference$Brand, Reference$Owner, "NULL")

Basically what I'm trying to do is to identify whether there is a brand from ExcelData to Reference. If there is then I'll get its owner

Example: In the ExcelData there is a brand 'Baja', there is also a brand 'Baja' in the reference file. Now with the logic of %in% I should get the owner of brand 'Baja' which in this case is 'AMBC01' but the owner that I get when running the code is different. Now I'm thinking that maybe I need to track the row or anything in order to get the right owner for each identified brand.

Upvotes: 1

Views: 27

Answers (1)

Artem
Artem

Reputation: 3414

It's better to use joins instead ofifelse and %in%. Different types of joins combine columns of 2 (or more tables) into one table. Based on your description it seems left join is feasible, please see the simulation and code below:

library(tibble)
library(tidyverse)
# Data simulation
excel_data <- tribble(~ brand, ~id,
                     "Baja", 1,
                     "Aumento", 4)
reference_data <- tribble(~brand, ~owner,
                          "Baja", "AMBC01",
                          "Aumento", "BMBC02",
                          "Azul", "CDCX03")

# Algorithm
left_join(excel_data, reference_data, by = "brand")

Output:

Joining, by = "brand"
# A tibble: 2 x 3
  brand      id owner 
  <chr>   <dbl> <chr> 
1 Baja        1 AMBC01
2 Aumento     4 BMBC02

Upvotes: 1

Related Questions