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: How to Randomly Assign 4 Integers to a Dataset?


From   "Seed, Paul" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: How to Randomly Assign 4 Integers to a Dataset?
Date   Fri, 12 Nov 2010 19:45:33 +0000

A two-line solution to Charlie's problem is

gen x = 5*int(4*runiform())
recode x 0 = 1

This does not guarantee equal sized groups, but the individual 
values of x are randomly selected from a uniform distribution.

Alternatively:
gen x = runiform()
sort x
replace x = 5*int(4*(_n-1)/_N)
recode x 0 = 1

This will give near-equal-sized groups


Paul Seed, Senior Lecturer in Medical Statistics, 
King's College London, Division of Women's Health


On Thu, Nov 11, 2010 at 6:17 PM, Carly Petracco <[email protected]> wrote:
>  I have 4 values that I want to randomly assign to the observations in
> my dataset.
>  I do not want this to occur over a range (I know about runiform), all
> I have 4 integers (i.e. 1, 5, 15, 20) that I want to randomly assign
> to my observations, how do I do that?
>  I have not been able to find a solution for this problem on the web
> or in previous questions, so any help would be greatly appreciates!
>
>  Best,
>  Charlie


*
*   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