I am just venturing into R programming and finding my way around.
Lets say I have a table as below:
Store | Product | Sales
X | A | 2
X | B | 1
X | C | 3
Y | A | 1
Y | B | 2
Y | C | 5
Z | A | 3
Z | B | 6
Z | C | 2
I need to change the sales values of certain products based on another table. Please find below:
Product | Sales
A | 10
B | 7
C | 15
My final table should be:
Store | Product | Sales
X | A | 10
X | B | 7
X | C | 15
Y | A | 10
Y | B | 7
Y | C | 15
Z | A | 10
Z | B | 7
Z | C | 15
I have 2 methods of doing this now: 1) Using joins 2) Using an if-else statement inside a for loop to subset the
Is there any other way to do this more effectively and in fewer steps? Thanks in advance!
EDIT: I forgot to mention an exception earlier. What if my dataset is like below?
Store | Product | Sales X | A | 2 X | B | 1 X | C | 3 X | D | 4 Y | A | 1 Y | B | 2 Y | C | 5 Y | D | 2 Z | A | 3 Z | B | 6 Z | C | 2 Z | D | 3
There's an extra product(D) with sales. I want to retain the value of sales for that product if it is not present in the 2nd table which is:
Product | Sales A | 10 B | 7 C | 15