'Create new value based on equation in R
I am new to R so I am struggling to figure out how to do this. I have a data frame with thousands of observations that looks like this
x total subgroup1 subgroup2
.03 1000 500 500
.04 1050 550 500
.07 1100 525 575
.08 9900 4900 5000
I am trying to come up with a weighted value for x based on the following equation.
weighted x for subgroup1 = (Σ x*subgroup1) / (Σ subgroup1)
The idea is that for each observation we multiply the x value by the subgroup 1 value and add them all together and divide it by the sum of all the subgroup values. I also want to do this for subgroup 2. I imagine I will need some type of loop function. However, I am not sure where to start in R. Any leads would be great.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
