mardi 16 juin 2020

using if else function to judge different calculation, then input accordingly in new variable

I know they say you are not suppose to use if else in data table because its bad coding practise. i'm not too advanced at data.table. my example wouldn't need a head(dput)) output.

# This didn't work
ins[, valid := ifelse((value_mid/quantity)*100 == price_mid, TRUE, FALSE)]
ins[, valid := ifelse((value_mid/quantity)*10 == price_mid, TRUE, FALSE)]
ins[, valid := ifelse((value_mid/quantity) == price_mid, TRUE, FALSE)]

#neither did this
ins[,valid := ((value_mid/quantity)*100 = price_mid) &
             ((value_mid/quantity)*10 = price_mid) &
  (value_mid/quantity = price_mid)]

how could i use if else without chnging the standard for judgement every time i call.

Thanks

Aucun commentaire:

Enregistrer un commentaire