Announcement

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

  • Partially transpose/ reshape variable in dataset

    Hello there,

    I have a data set like this-

    uid question c1 c2 c3

    1 q1 2 3 6
    1 q2 3 5 2
    1 q3 4 5 3
    2 q1 5 2 6
    2 q2 3 6 2
    3 q1 3 4 5
    3 q2 3 5 2
    3 q3 3 3 6
    3 q4 4 5 2

    But, I need it like this-

    uid class q1 q2 q3 q4

    1 c1 2 3 4 .
    1 c2 3 5 5 .
    1 c3 6 2 3 .
    2 c1 5 3 . .
    2 c2 2 6 . .
    2 c3 2 3 4 .
    3 c1 3 3 3 4
    3 c2 4 5 3 5
    3 c3 5 2 6 2

    I have tried xpose, sxpose, reshape and stack.
    How can I do it? Thanks in advance.

  • #2
    Code:
    reshape long c, i(uid question) j(class)
    reshape wide c, i(uid class) j(question) string

    Comment


    • #3
      I tried this, didn't work. The message showed "no xij variables found". But thank you though.
      However, I have solved it by creating another data file with unique identifiers and question no. where there was no gap in the question number, merged both data files (new and old), then xpose and later stack.
      Thank you again.

      Comment


      • #4
        I tried the data from #1 (making a guess that question is a string variable: #1 lacks a proper data example, as explained at FAQ Advice #12) and the code from #2 from Wouter Wakker.

        Looks good to me!

        Hence a report of "didn't work" needs more documentation than an error message: we need to see the code before it.

        Code:
        clear
        input uid str2 question c1 c2 c3
        1 q1 2 3 6
        1 q2 3 5 2
        1 q3 4 5 3
        2 q1 5 2 6
        2 q2 3 6 2
        3 q1 3 4 5
        3 q2 3 5 2
        3 q3 3 3 6
        3 q4 4 5 2
        end
        
        reshape long c, i(uid question) j(class)
        reshape wide c, i(uid class) j(question) string
        
        
        list, sepby(id)  
        
              
             +-------------------------------------+
             | uid   class   cq1   cq2   cq3   cq4 |
             |-------------------------------------|
          1. |   1       1     2     3     4     . |
          2. |   1       2     3     5     5     . |
          3. |   1       3     6     2     3     . |
             |-------------------------------------|
          4. |   2       1     5     3     .     . |
          5. |   2       2     2     6     .     . |
          6. |   2       3     6     2     .     . |
             |-------------------------------------|
          7. |   3       1     3     3     3     4 |
          8. |   3       2     4     5     3     5 |
          9. |   3       3     5     2     6     2 |
             +-------------------------------------+

        Comment


        • #5
          Sorry, for the mistake Nick Cox. I am new in this forum and as well as with Stata.
          I presented a model of my dataset as an example in #1. So, there were a few issues with the variable names in my original data. They were not named sequentially. I renamed them accordingly. Then were was a little issue as my 'question' variable was not string. So, I made the change and lastly it worked just fine. Thank you Wouter Wakker and Nick Cox. And, sorry again.

          Code:
          . rename f1_1 question
          . rename f1_3 f1
          . rename f1_4 f2
          . rename f1_5 f3
          . rename f1_6 f4
          . rename f1_7 f5
          . reshape long f, i(q_sl_no question) j(class)
          (note: j = 1 2 3 4 5)

          Data wide -> long

          Number of obs. 2245 -> 11225
          Number of variables 7 -> 4
          j variable (5 values) -> class
          xij variables:
          f1 f2 ... f5 -> f

          . reshape wide f, i(q_sl_no class) j(question)
          (note: j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)

          Data long -> wide

          Number of obs. 11225 -> 800
          Number of variables 4 -> 17
          j variable (15 values) question -> (dropped)
          xij variables:
          f -> f1 f2 ... f15

          Comment

          Working...
          X