Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create a 4 categories variable from a existing two categories one?

    Hello there, I have a variable that is coded into two values, whereas in each one the label of that value has two different possible answers. I want to split this variable into four values (assigning the 50% of the original value to each of the new ones).

    So, for example, if the original variable had values 1(with 10 obs) and 2(with 10 obs), I want to create a new variable with 4 values, each of them with 5 obs.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double p97_1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    1
    2
    1
    2
    2
    2
    2
    2
    2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    1
    2
    2
    2
    1
    2
    2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    end
    label values p97_1 P97_1
    label def P97_1 1 "Dif�cil o pr�cticamente imposible", modify
    label def P97_1 2 "F�cil o muy f�cil", modify

  • #2
    Well, you don't say what the other values are supposed to be! I'll assume that you want to split the 1's into 1 and 3, and the 2's into 2 and 4.
    Code:
    gen long obs_no = _n
    set seed 1234 // OR ANY INTEGER SEED YOU LIKE
    gen double shuffle = runiform()
    by p97_1 (shuffle), sort: replace p97_1 = p97_1 + 2 if 2*_n <= _N
    sort obs_no
    Note: This code will change the value of exactly half (or as close as possible if the total number is odd) of the values, selected at random. This is not exactly the same as selecting a random subset with 0.5 probability--in which case on average half would be changed, but in any given trial it could be somewhat more or fewer. You didn't make clear which you wanted, so I just guessed.

    Comment


    • #3
      Thanks a lot!!!

      Comment

      Working...
      X