mardi 28 mai 2019

Using R, if values in either variable A or B are 'NA', to then use values in variables C and D instead?

I am trying to calculate the mean of two variables and outputting it as a new variable. For example:

data$Average <- (data$Visit1 + data$Visit2) / 2

However, there are NA values for both 'Visit 1' and 'Visit 2' variables across the dataset.

How can I write some code such that I am able to identify if NA values exist in either Visit 1 or 2, then I would use the corresponding row's values but for 'Visit 3' and 'Visit 4' instead, to calculate the average?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire