Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: egen rowmean, loops and if


From   Thomas Speidel <[email protected]>
To   [email protected]
Subject   Re: st: egen rowmean, loops and if
Date   Tue, 05 Apr 2011 20:33:36 -0600

Thanks Nik. I think I will choose the reshape option: much more appealing. You mentioned the word "temporarily" earlier. I am aware of preserve/restore. In general, what advice what you give (I have some 150 variables): reshape the whole dataset back and forth?

------------------------------------------------------------------------

	Nick Cox <mailto:[email protected]>
April-05-11 18:09


Here is example code for a -reshape- solution.

clear
set obs 10
forval j = 1/3 {
forval i = 1/8 {
gen occ_met`j'_`i' = runiform()
}
}

gen id = _n
reshape long occ_met, i(id) string
split _j, parse(_) destring
rename _j1 i
rename _j2 j
egen mean1 = mean(occ_met) if occ_met > 0.5 , by(j)
egen mean2 = mean(occ_met) if occ_met <= 0.5 , by(j)


*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
------------------------------------------------------------------------

	Nick Cox <mailto:[email protected]>
April-05-11 17:59


Here is example code for a long-winded solution:

clear
set obs 10
forval j = 1/3 {
forval i = 1/8 {
gen occ_met`j'_`i' = runiform()
}
}
ds

forval i = 1/8 {
gen mean1_`i' = 0
gen mean2_`i' = 0
gen n1_`i' = 0
gen n2_`i' = 0
qui forval j = 1/3 {
replace mean1_`i' = mean1_`i' + occ_met`j'_`i' if occ_met`j'_`i' > 0.5
replace n1_`i' = n1_`i' + (occ_met`j'_`i' > 0.5)
replace mean2_`i' = mean2_`i' + occ_met`j'_`i' if occ_met`j'_`i' <= 0.5
replace n2_`i' = n2_`i' + (occ_met`j'_`i' <= 0.5)
}
replace mean1_`i' = mean1_`i' / n1_`i'
replace mean2_`i' = mean2_`i' / n2_`i'
}



*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
------------------------------------------------------------------------

	Nick Cox <mailto:[email protected]>
April-05-11 17:37


This would be a lot easier if you -reshape-d, even temporarily.

Otherwise, with this data structure: -egen, rowmean()- is a
non-starter here and I think you need to work at a lower level,
building up sums and counts and deriving means.

A side-detail is that -foreach- is not needed here: use -forval- instead.

Nick

*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index