0

I have a df in R that I am trying to perform calculations on within a function.

I have computed change_price, change_quant, change_profit

What I am trying to do is select the old quantity normq if change_price and change_quant are both greater than 1 and select the new quantity NormQ if change_price > 1 & change_quant <= 1

So far I have tried several methods that dont seem to work properly, including:

  inData$norm_quant <- ifelse(inData$change_price >= 1 & inData$change_quant <= 1, inData$NormQ, inData$normq)

  inData$norm_quant <- ifelse(inData$change_price >= 1 && inData$change_quant <= 1, inData$NormQ, inData$normq)

  inData$norm_quant <- ifelse(inData$change_price >= 1 | inData$change_quant <= 1, inData$NormQ, inData$normq)

Ive tried so many other ways its pointless to list them all here. Point is, how can I correctly select the desired quantity based on the logical idea that price and quantity cannot both increase (so select the old quantity if they do but keep the new price)?

I know there is another thread on this subject, but its conclusions were not helpful to me so I am reposting this question.

> head(opt1)
   sensitivity      PRODUCT  Revenue Quantity     Profit       cost norm_price  norm_quant norm_prof change_prof change_quant change_price    final_rev final_profit
1:           1 PRODUCT10000   563.04     2672   362.4716 0.31567063  0.8861575 0.402326322 0.7057182   0.5041516    0.3252316    1.3541611 0.3565244903 0.2295218855
2:           1 PRODUCT10001  9291.51    73978  6733.7684 0.31664415  1.1502735 0.402326322 0.4348806   0.8172296    0.7712255    1.0432302 0.4627852903 0.3353910144
3:           1 PRODUCT10002 15015.24    26048 13071.6165 0.13677461  1.1000000 0.092918066 0.4284446   5.0127018    4.7870351    1.0410391 0.1022098726 0.0895010400
4:           1 PRODUCT10003   620.54      112   565.3049 0.08194787  1.1000000 0.213661313 0.4528317   2.5270022    2.0818090    1.1948142 0.2350274446 0.2175183551
5:           1 PRODUCT10004  2154.00     1000  1605.0000 0.48488348  1.3000000 0.000464435 0.2933202 445.5299526  774.8137110    0.6833333 0.0006037655 0.0003785686
6:           1 PRODUCT10005  1390.00    10000  1028.2500 0.27736785  1.2000000 0.001318247 0.3711992 357.1610035  305.1980502    1.1259494 0.0015818960 0.0012162568

In the above example, lines 3, 4, and 6 should return the old quantity.

zsad512
  • 861
  • 3
  • 15
  • 41
  • What if they're both < 1? – IceCreamToucan Nov 28 '17 at 16:23
  • @RobJensen that shouldnt happen, but if it does then select `normq, normp` (the old values) instead of `NormQ, NormP` – zsad512 Nov 28 '17 at 16:24
  • Can you provide us with a full [working example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example-aka-mcve-minimal-complete-and-ver) including a dataset so that we can actually try out different things? And can you also tell us what result you are getting? Like, are you getting an error? Wrong values? If so in which conditions do you get the wrong values? – Arthur Spoon Nov 28 '17 at 16:36
  • @ArthurSpoon I added some sample data and a little explanation, hope thats helpful – zsad512 Nov 28 '17 at 16:57
  • there is no `normq` or `NormQ` in the data set. Also, take a look at `?'&'` as it explains that `&` performs elementwise comparisons. – manotheshark Nov 28 '17 at 17:55

0 Answers0