Announcement

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

  • How to average duplicate variables so that I can reshape data

    Hey guys,

    This is the data I have -

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float piece_prod str15 variable str2 index
     5555.556 "roadside_market" "1" 
     3333.333 "roadside_market" "1" 
     7777.778 "broker"          "2" 
    4444.4443 "broker"          "2" 
    1111.1111 "Retailers"       "2" 
     1277.778 "broker"          "3" 
    111.11111 "broker"          "3" 
    1111.1111 "broker"          "4" 
    1333.3334 "roadside_market" "5" 
     777.7778 "roadside_market" "5" 
    1111.1111 "broker"          "6" 
     888.8889 "broker"          "6" 
     444.4445 "broker"          "6" 
     333.3333 "broker"          "6"
    This is the code I used, but it didn't work -

    Code:
    reshape wide piece_prod, i(index) j(variable) string
    How do I average piece_prod values when index and variable is same, so that I can finally reshape it?

    ​​​​​​​Thanks.


  • #2
    Code:
     collapse (mean) piece_prod, by(index variable)
     reshape wide piece_prod, i(index) j(variable) string
    Are you sure you want to do this? Most data management and analysis runs more easily in long layout than wide in Stata. The wide layout is primarily useful for creating a data set that, in the browser, is easier for human eyes to read, for some graphs, and a handful of other commands. But most of Stata works best with data in long layout. So, the code above shows you how to get to wide layout, but think twice whether doing so is really a good idea.

    Comment

    Working...
    X