mardi 28 novembre 2017

Proper use of ifelse with & or &&

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.

Aucun commentaire:

Enregistrer un commentaire