I am trying to calculate the weighted average of a statistical sample (vector) in R using this form:

The function takes a vector, and the weight is adjusted in accordance with the second parameter (1 - 3), which:

where sis the standard deviation.
I adjusted the weight accordingly if the parameter is 1 or 3 using else-if, but I am having problems with the second, if you have criteria to satisfy ...
I calculated X - xBaras a vector:m = x-mean(x)
I calculated susing the function R :s = sd(x)
, " " . if , ...
( , ,), x (m/s) 1? 3 ?
. 1, 1, 1 2, 0,5?
, . R , , ... , .
.