Announcement

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

  • reshaping from Long to Wide

    I have a data in this form
    ID_farmer Crop_class class_ID Class_value farmer_tot
    1 cereals cereals
    1 forage forage
    1 fruit_tree fruit_tree 200 680
    1 hoticulture crop hoticulture crop
    1 industrial crop industrial crop
    1 legumes crop legumes crop
    1 other crop other crop
    1 root crop root crop 480 680
    2 cereals cereals 1590 4130
    2 forage forage
    2 fruit_tree fruit_tree 540 4130
    2 hoticulture crop hoticulture crop
    2 industrial crop industrial crop
    2 legumes crop legumes crop
    2 other crop other crop
    2 root crop root crop 2000 4130
    5 cereals cereals 3675 3675
    5 forage forage
    5 fruit_tree fruit_tree
    5 hoticulture crop hoticulture crop

    storage display value
    variable name type format label variable label
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    ID_farmer float %9.0g group(CONGLOMERADO NSELUA UA)
    Crop_class str16 %16s Crop classification
    class_ID int %16.0g class_ID group(Crop_class)
    Class_value float %9.0g
    farmer_tot float %9.0g







    How do I reshape it into something like this
    ID_farmer Crop_class cereals forage fruit_tree hoticulture crop industrial crop legumes crop other crop root crop farmer_tot
    1 200 480 680
    2 150 540 2000 4130
    5 3675 3675

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id_farmer str16(crop_class class_id) int(class_value farmer_tot)
    1 "cereals"          "cereals"             .    .
    1 "forage"           "forage"              .    .
    1 "fruit_tree"       "fruit_tree"        200  680
    1 "hoticulture crop" "hoticulture crop"    .    .
    1 "industrial crop"  "industrial crop"     .    .
    1 "legumes crop"     "legumes crop"        .    .
    1 "other crop"       "other crop"          .    .
    1 "root crop"        "root crop"         480  680
    2 "cereals"          "cereals"          1590 4130
    2 "forage"           "forage"              .    .
    2 "fruit_tree"       "fruit_tree"        540 4130
    2 "hoticulture crop" "hoticulture crop"    .    .
    2 "industrial crop"  "industrial crop"     .    .
    2 "legumes crop"     "legumes crop"        .    .
    2 "other crop"       "other crop"          .    .
    2 "root crop"        "root crop"        2000 4130
    5 "cereals"          "cereals"          3675 3675
    5 "forage"           "forage"              .    .
    5 "fruit_tree"       "fruit_tree"          .    .
    5 "hoticulture crop" "hoticulture crop"    .    .
    end
    
    drop class_id
    replace crop_class= strtoname(crop_class)
    reshape wide class_value farmer_tot, i(id_farmer) j(crop_class) string
    egen total= rowmax(farmer_tot*)
    drop farmer_tot*
    rename class_value* *
    Res.:

    Code:
    . l
    
         +-------------------------------------------------------------------------------------------------------+
         | id_far~r   cereals   forage   fruit_~e   hoticu~p   indust~p   legume~p   other_~p   root_c~p   total |
         |-------------------------------------------------------------------------------------------------------|
      1. |        1         .        .        200          .          .          .          .        480     680 |
      2. |        2      1590        .        540          .          .          .          .       2000    4130 |
      3. |        5      3675        .          .          .          .          .          .          .    3675 |
         +-------------------------------------------------------------------------------------------------------+
    Last edited by Andrew Musau; 28 Apr 2022, 15:40.

    Comment


    • #3
      Thank you Andrew Musau.

      Comment

      Working...
      X