Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: generate new variable with frequencies


From   Ulrich Kohler <kohler@wz-berlin.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: generate new variable with frequencies
Date   Tue, 4 Nov 2003 14:55:33 +0100

How about:

. gen N = _N 
. by typ, sort: gen n = _N
. gen f = n/N

regards
uli

Matteo Foschi wrote:
> We have a little trouble with a, we think, easy task.
> We want to generate a variable, which contains the relative frequency of
> another variable values.
> We have a variable, say “type”, and want to built a new variable,
> say “typ_freq”, which shows the relatively frequency of each value of
> “type”.
>
> We have tried first with the tablepc ado-file:
> tablepc type, generate (typ_freq)
> Therefore we obtain only the relatively frequency of each observation.
>
> We can obtain - in alternatively - the cumulated frequency (variable
> freqcum) with a little program:
>
> Generate freqcum =.
> ..
> sort typ_freq
> by typ_freq: gen groups = 1 if _n ==1
> replace groups = sum(groups)
> ..
> local K = groups[_N]
> local i 1
> while `i' <= `K' {
>  replace freqcum = sum(typ_freq)
>  local i = `i' + 1
> }
> ..
> We are not able, however,  to recode freq_cum or typ_freq in order to
> obtain the relatively frequency of each value of “type”, as bottom shows:
>
> Obs	Type  obs_freq  freq_cum   		typ_freq
> 1	1	1		1		2
> 2	1	1		2		2
> 3	2	1		3		3
> 4	2	1		4		3
> 5	2	1		5		3
> 6	3	1		6		1
> 7	4	1		7		2
> 8	4	1		8		2
> 9	5	1		9		1
> 10	6	1		10		1
>
>
> Could anyone suggest what we have to do?
>
> Thanks
>
> M. Schommer  /  M. Foschi
> schommer@rumms.uni-mannheim.de
> foschi@rumms.uni-mannheim.de
> ++49.(0)621.181-2011
> Fakultät für Sozialwissenschaften
> University of Mannheim
> Germany
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

-- 
kohler@wz-berlin.de


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index