I'm becoming mad because I'm using the sum() function and it is showing different results without sense. I have 4 numerical variables: A, B, M, N. Also I have a weights variable: W.
If I make the weighted sum:
sum(df$W * (df$A), na.rm = T) = AR
sum(df$W * df$A, na.rm = T) = AR
The result is the same.
If I add B:
sum(df$W * (df$A + df$B), na.rm = T) = ABR
sum(df$W * df$A, df$W * df$B, na.rm = T) = ABR
The result is the same.
If I add M:
sum(df$W * (df$A + df$B + df$M), na.rm = T) = ABMR1
sum(df$W * df$A, df$W * df$B, df$W * df$M, na.rm = T) = ABMR2
The result become different.
If I add N:
sum(df$W * (df$A + df$B + df$M + df$N), na.rm = T) = ABMNR1
sum(df$W * df$A, df$W * df$B, df$W * df$M, df$W * df$N, na.rm = T) = ABMNR2
The result is different.
So it seems the M and/or N variable have some problem. BUT, if I start adding M and N variable...:
sum(df$W * (df$M), na.rm = T) = MR
sum(df$W * df$M, na.rm = T) = MR
The result is the same.
If I add N:
sum(df$W * (df$M + df$N), na.rm = T) = MNR
sum(df$W * df$M, df$W * df$N, na.rm = T) = MNR
The result is the same.
Now, if I add A:
sum(df$W * (df$M + df$N + df$A), na.rm = T) = MNA1
sum(df$W * df$M, df$W * df$N, df$W * df$A, na.rm = T) = MNA2
The result become different.
If I add B:
sum(df$W * (df$M + df$N + df$A + df$B), na.rm = T) = MNAB1
sum(df$W * df$M, df$W * df$N, df$W * df$A, df$W * df$B, na.rm = T) = MNAB2
The result is different.
Now it seems the problem comes from A or B variables. How is it possible? Is there any difference if I make the sum multiplying the W variable with the sum of the variables (first way), or if I add the different variables (second way)?
Thank you very much for any help you con provide!