I have a data frame Mesure
and I wish to determine for each row, a weighted mean like this :
weighted_mean = ((mean_Mesure x nbr_Mesure) + (mean_DL x nbr_DL)) / (nbr_Mesure + nbr_DL)
I know there is a weighted.mean
function but I failed to obtain a new column "weighted_mean"
And, is it an issue if each row does not necessary have the 4 values to obtain this formula (such as row 6 in Mesure
) ?
> head(Mesure)
Row.names mean_Mesure nbr_Mesure mean_DL nbr_DL
2 Aquatic_moss.BE-7 123 4 542 12
3 Aquatic_moss.CO-57 100 7 117 14
4 Aquatic_moss.CO-58 120 5 145 12
5 Aquatic_moss.CO-60 140 5 153 12
6 Aquatic_moss.CS-134 146 15