set.seed(2022)
df <- data.frame(key1=rep(c('a','b'),each=2),
key2=rep(c('x1','x2'),2),
v1=runif(4),
v2=runif(4)
)
I want to calculate new values x3=x2/x1 for each key1,then append to primary dataframe.The total result I want such as:
key1 key2 v1 v2
1 a x1 0.6702825 0.6736447
2 a x2 0.2144166 0.9945389
3 b x1 0.7663272 0.3333884
4 b x2 0.9322578 0.9882694
5 a x3 x2/x1 x2/x1 <---new values
6 b x3 x2/x1 x2/x1 <---new values
Thx!