0

I'm sure this question will seem a little bit basic for many but here is my problem:

I want to create a new variable which is an equation of other variables in RStudio:

D$satisfaction.conditions <- (D$imp.distance * D$sat.distance
                          + D$imp.salaire * D$sat.salaire
                          + D$imp.horaires * D$sat.horaires
                          + D$imp.chargetravail * D$sat.chargetravail
                          + D$imp.nbservice * D$sat.nbservice
                          + D$imp.locaux * D$sat.locaux
                          + D$imp.equipements * D$sat.equipements
                          + D$imp.ambiance * D$sat.ambiance
                          + D$imp.relationcollegues * D$sat.relationcollegues
                          + D$imp.stress * D$sat.stress) 

The issue is that I have some missing values in the equation, so I get a NA result for some observations.

I know that there is something to do with na.rm=TRUE but I can't find where to put it. I tried at the end but I get a

Error: unexpected symbol in:
"                          + D$imp.relationcollegues * D$sat.relationcollegues
                          + D$imp.stress * D$sat.stress) na.rm"

How can I get my new variable {satisfaction.conditions} ommiting NA values ?

Tom
  • 445
  • 1
  • 4
  • 10
wikninja
  • 3
  • 1
  • Do you need the answer to be 0 (where NA exist) or do you want to calculate and then remove the NA rows? – SmitM Sep 13 '18 at 13:42
  • I want to calculate without the NAs where there is missing values. I don't want the result to be NA. – wikninja Sep 13 '18 at 14:01
  • In that case, you need to replace the NAs with 0s before running your code – SmitM Sep 13 '18 at 14:11

1 Answers1

1

Add this line of code before performing the calculation:

D[is.na(D)] <- 0

This will replace all the NAs with 0s

SmitM
  • 1,366
  • 1
  • 8
  • 14