Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Shuffling elements of a string variable within cases


From   Michael Lacy <[email protected]>
To   [email protected]
Subject   st: Shuffling elements of a string variable within cases
Date   Tue, 16 Jan 2007 15:42:56 -0700 (MST)

I'd like to randomly reorder (i.e., shuffle) the elements of a string
variable within each case.

For example,with a string variable of length 6

id  s
1   abcdef
2   abcdef
3   abcdef

I'd like to produce, say:

id  s
1   fecdab
2   cabfde
3   fbcade

The best approach I can come up with is to break the string into component str1 variables s1 to
s6, and then do

reshape long s, i(id) j(j)
gen tempran = uniform()
sort id tempran
by id: replace j = _n
drop tempran
reshape wide x, i(id) j(j)

Using -reshape- seems something of an overkill, and sorting on a random number is not a very
efficient shuffling algorithm for a long length string. I'm aware of -shuffle-, but it is designed
for a list rather than a variable. Can someone suggest a better approach?


Regards,
Mike Lacy
Fort Collins CO

=-=-=-=-=-=-=-=-=-==-=-=-=
Mike Lacy,  Ft Collins CO 80523
voice (970) 491-6721
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index