I have a data frame like this one:
ID duration1 duration2 total_duration quantity1 quantity2
1 5 2 7 3 1
2 NA 4 4 3 4
3 5 NA 5 2 NA
I would like to do a weighted mean for each subject like this:
df$weighted_mean<- ((df$duration1*df$quantity1) + (df$duration2*df$quantity2) ) / (df$total_duration)
But as I have NA, this command does not work and it is not very nice....
The result would be this:
ID duration1 duration2 total_duration quantity1 quantity2 weighted_mean
1 5 2 7 3 1 2.43
2 NA 4 4 3 4 4
3 5 NA 5 2 NA 2
Thanks in advance for the help