vendredi 24 mai 2019

How to do a calculation in case the column exist or not in R?

Here my example;

  var1   var2 var3? var4? var5?  c  var1_C   var2_C
   5      8                      10   50       400
   7      9                      20   140      1260

In this case, I need a query that not only multiply var1 and var2 per C, if there is var3 that multiplies it by C but if it does not exist that puts NA

Like this

var1  var2  var3  var4  var5  C var1_C Var2_C var3_C var4_C Var5_C
  5    8     NA    NA    NA       50     400    NA     NA     NA
  7    9     NA    NA    NA       140    1260   NA     NA     NA

Aucun commentaire:

Enregistrer un commentaire