Announcement

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

  • Questions: reshape to long format for three conditions

    Hi all,
    I have three conditions (1, 2, 3) and for each condition, people answered 30 same questions.
    My original data looks like this:
    id condition1_01 condition1_02 ... condition2_01 condition2_02 ... condition3_01 condition3_02
    1
    2
    3
    4
    I want to reshape my data to long format, like this:
    id condition Q1 Q2 Q3 ... Q30
    1 1
    1 2
    1 3
    2 1
    Is reshape command able to do this and how? Thanks in advance!
    Best Regards,
    Cecilia

  • #2
    Welcome to Statalist. Please review the FAQs by clicking on the button at the top left-hand side of the screen, especially the section on how to present data examples using the dataex command. If the following does not give you what you want, present a data example.

    Code:
    rename (condition1_0* condition2_0* condition3_0*)  (condition1_* condition2_* condition3_*)
    gen condition=1
    preserve
    keep id condition condition2_*
    rename (condition2_*) (Q#), addnumber(1)
    replace condition=2
    tempfile two
    save `two'
    restore, preserve
    keep id condition condition3_*
    rename (condition3_*) (Q#), addnumber(1)
    replace condition=3
    tempfile three
    save `three'
    restore, preserve
    keep id condition condition1_*
    rename (condition1_*) (Q#), addnumber(1)
    append using `two'
    append using `three'
    order id condition
    sort id condition
    Last edited by Andrew Musau; 28 May 2019, 08:06.

    Comment


    • #3
      An similar approach, shorter but requires typing "Q1 Q2 ... Q30" on the reshape command. In this example I reduced the names from condition1_01 to c1_01 etc. and reduced the number of questions from 30 to 04 in order to make it easier to post readable sample data.
      Code:
      cls
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int(id c1_01 c1_02 c1_03 c1_04 c2_01 c2_02 c2_03 c2_04 c3_01 c3_02 c3_03 c3_04)
      1 111 112 113 114 121 122 123 124 131 132 133 134
      2 211 212 213 214 221 222 223 224 231 232 233 234
      3 311 312 313 314 321 322 323 324 331 332 333 334
      end
      rename (c#_#) (q#[2]#[1])
      reshape long q01 q02 q03 q04, i(id) j(cond)
      list, noobs sepby(id)
      Code:
      . list, noobs sepby(id)
      
        +-----------------------------------+
        | id   cond   q01   q02   q03   q04 |
        |-----------------------------------|
        |  1      1   111   112   113   114 |
        |  1      2   121   122   123   124 |
        |  1      3   131   132   133   134 |
        |-----------------------------------|
        |  2      1   211   212   213   214 |
        |  2      2   221   222   223   224 |
        |  2      3   231   232   233   234 |
        |-----------------------------------|
        |  3      1   311   312   313   314 |
        |  3      2   321   322   323   324 |
        |  3      3   331   332   333   334 |
        +-----------------------------------+

      Comment


      • #4
        Thank you, Andrew and William! It helps a lot! I will try to get familiar with the forum and do more searching before post :p
        Last edited by Cecilia Chen; 29 May 2019, 01:18.

        Comment

        Working...
        X