-3

I have two dataframes in my program, one of which stores the information about purchases (including product IDs), the other stores information about products, (product IDs and brand names(as factor)) I want to add to 1st dataframe column with brand names for each product. I want something like:

purchases$brand <- products[purchases$product_id == products$id,]$brand 
Sandy
  • 1,100
  • 10
  • 18
artem zholus
  • 309
  • 3
  • 9
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jun 05 '16 at 20:03
  • You may benefit by looking at this: http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – Gopala Jun 05 '16 at 20:26

1 Answers1

1

Use %in% operator, below command will add new column Brand to purchases data frame based on the matching product$id and purchases$product_id

 purchases$brand <- products$brand[products$id %in% purchases$product_id]
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30