For example, in the table below (a sample of my dataset is provided at the end) the average I want for GEO=YRP and URB_CL=LOW is 6.3 (=(11.305+1.391)/2), rather than 8.0 (=(11.305+11.305+1.391)/3).
I would like to have the correct average as a new variable, knowing that in this case 6.3 will be the value for all instances when GEO=YRP and URB_CL=LOW.
geo | iso | urb_cl | urb | curr |
YRP | ASA | low | 11.305 | 4502.119 |
YRP | ASA | 11.305 | ||
YRP | ASA | low | 11.305 | 7127.425 |
YRP | KWW | 0 | ||
YRP | REQ | low | 1.391 | 8219.178 |
YRP | TRS | high | 66.793 | 2925.037 |
Code:
table geo urb_cl if !missing(curr), c(n urb mean urb ) format(%14.1fc) cellwidth(10)
Code:
preserve collapse urb urb_cl if !missing( curr ), by( iso geo ) sort geo iso urb table geo urb_cl, c(n urb mean urb ) format(%14.1fc) cellwidth(10) restore
------------------
My dataset:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str3(geo iso) float urb_cl double urb float curr "PAC" "ASA" 2 82.892 1873.3757 "PAC" "ASA" 2 82.892 1252.2163 "PAC" "BGD" 2 68.775 3583.354 "PAC" "FJI" . 47.317 . "PAC" "KHM" 2 79.337 7045.824 "PAC" "VNM" 2 68.92 1321.158 "PAC" "VNM" 2 68.92 4199.222 "YRP" "ASA" 0 11.305 4502.1187 "YRP" "ASA" . 11.305 . "YRP" "ASA" 0 11.305 7127.425 "YRP" "KWW" . 0 . "YRP" "REQ" 0 1.391 8219.178 "YRP" "TRS" 2 66.793 2925.0366 end label values urb_cl label_n label def label_n 0 "low", modify label def label_n 2 "high", modify
0 Response to Average within a group for each unique combination
Post a Comment