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: Understanding Factor variables - is order significant ?


From   "Feiveson, Alan H. (JSC-SK311)" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Understanding Factor variables - is order significant ?
Date   Thu, 27 May 2010 10:54:07 -0500

Austin - Yes, of course you're correct. Thanks for pointing this out - and also Nick - thanks for mentioning the label option on encode.

Al

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Austin Nichols
Sent: Thursday, May 27, 2010 10:11 AM
To: [email protected]
Subject: Re: st: Understanding Factor variables - is order significant ?

Al--
I think you meant to use -encode- in your example, since you may not
label string variables (http://www.stata.com/search.cgi?query=181).
You could also just use sort order to establish the order of your new
variable, if setting up the sort order is simpler in some applications
(this is a fairly simple way to handle a very large number of string
levels):

clear
input str20 svar
"Low"
"Medium Low"
"Medium"
"Medium High"
"High"
end
forv i=1/`=_N' {
la def o `i' "`=svar[`i']'", modify
}
encode svar, g(nvar) label(o)
list
list, nol

On Thu, May 27, 2010 at 10:52 AM, Feiveson, Alan H. (JSC-SK311)
<[email protected]> wrote:
> Rich - I agree.
>
> OK - How about an "order" option on encode:
>
> encode stringvar,order(Low Medium High)
>
>
> HA!
>
>
> This would replace doing
>
> label define level 1 "Low" 2 "Medium" 3 "High"
> label values stringvar level
>
> (maybe a trivial amount of extra work in this example, but not if you had a list of 20 levels or so and had to list them all  with numbers and double quotes around each one)
>
>
> Al F.
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Richard Williams
> Sent: Thursday, May 27, 2010 9:44 AM
> To: [email protected]
> Subject: RE: st: Understanding Factor variables - is order significant ?
>
> At 10:38 AM 5/27/2010, Feiveson, Alan H. (JSC-SK311) wrote:
>>The trouble with -encode- is that it puts the levels in alphabetical
>>order. So in the L, M, H case you would get H = 1, L = 2, M = 3. To
>>get around this you would have to define a label by hand and assign
>>it to the string variable.
>>
>>Al F.
>
> True, but as Nick points out you would have the same problem with
> factor variables.  One way or another you have to do a little extra
> work to get the codes in the order you want if they aren't in that
> order already.

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