Given this dataframe
df <- data.frame(mkt=c(1,1,1,1,2,2,2,2,2), mdl=c('a','a','b','b','b','a','b','a','b'), pr=c(120,120,110,110,145,130,145,130, 145)) df mkt mdl pr 1 1 a 120 2 1 a 120 3 1 b 110 4 1 b 110 5 2 b 145 6 2 a 130 7 2 b 145 8 2 a 130 9 2 b 145 I'd like to group by mkt and mdl and then calculate mean of pr, for all other groups in the market but the mdl I am calculating against. So for 1st group by mkt=1 and mdl=a, the avgother column should have average of mkt=1 and mdl=b.
Example below:
mkt mdl pr avgother 1 1 a 120 110 2 1 a 120 110 3 1 b 110 120 4 1 b 110 120 5 2 b 145 130 6 2 a 130 145 7 2 b 145 130 8 2 a 130 145 9 2 b 145 130 https://stackoverflow.com/questions/66837762/is-there-a-way-to-calculate-average-of-all-groups-but-the-current-group March 28, 2021 at 09:56AM
没有评论:
发表评论