Taking a Conditional Mean in Stata
Solution 1
There are probably a few ways to do this but this is what I'd suggest.
gen newvar = .
forvalues i = 1/10 {
qui sum price if type == `i', meanonly
replace newvar = r(mean) if type == `i'
}
Solution 2
Here's a different approach that is more simple and efficient. If you've got a large dataset, this will be faster than the multi-step loop aTron suggested and this approach adapts to changes in the range of your "type" variable (if your dataset changes in size, you don't have to go back through your code and change the range in the forvalues
command).
1) Create a fake dataset
clear
input type price
1 1000
2 3200
3 5000
4 1200
5 1000
1 4000
2 2000
3 4000
4 1200
5 2000
end
2) Generate the average price
by type
bysort type: egen meanprice = mean(price)
li type price meanprice, sepby(type)
Solution 3
You can create the means with
by type: egen conditional_mean = mean(price)
Jack7890
Updated on June 04, 2022Comments
-
Jack7890 almost 2 years
Let's say I have a Stata dataset that has two variables:
type
andprice
. Thetype
value for each observation is a number between 1 and 10.I want to add a third value that is the average
price
of all variables of thattype
. So, for example, if the first observation had atype
of 3 and aprice
of 10, then I'd like to add a third value that is the averageprice
of all observations withtype
=3.How can I do this in Stata?