I have a dataset below:
x =
BC_aitk90_0-5cm BC_aitk98_0-5cm BC_aitk98b_0-5cm BC_nn_0-5cm
1: 1.6382072 4.281057 4.607484 15.181894
2: 2.2413662 5.136081 5.341605 16.192835
3: 1.0995348 3.881533 3.493945 12.063029
4: 1.3492518 4.219009 3.818655 13.996832
5: 2.9033433 6.072661 6.149606 17.826441
I want to calculate row weighted mean, but I could not get the code and answer. My weight is:
x =
W = c(0.83, 0.85, 0.87, 0.93)
Here is two answers :
df = data.frame(a = c(1:3), b= c(4:6))
w = c(0.5,0.3)
wm1 = t(colSums(apply(df,1,function(x) x*w)) / sum(w))
# or
wm2 = apply(df,1,function(x) weighted.mean(x,w))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.