Announcement

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

  • Conditional row total

    Here is some part of my dataset. this data about the % of area of any plot sown under crop in specific season of a year. let for example in row(IMHC0055" 2009 "AB" "Kharif"). farmer cultivated two crops cotton and pigeon pea in plot AB, but he cultivated two varieties of pigeon pea in this plot. variable starting with PRCT_AREA tells about percentage of plot area under specified variety of crop.
    for above plot farmer cultivated SUPER MAROTI variety of cotton in 83.32 % area and two varieties of pigeon pea "ICPL-88039" in 8.34%" area and "MAROTI" in 8.34%" area. i want to generate a variable variable that will tell 5 area under specific crop per plot.
    for this I tried like this.
    generate pigeonpea_prct_area = PRCT_AREAvariety1crop1 *( CROPcrop1 == "pigeonpea") + PRCT_AREAvariety1crop2 *(CROPcrop2 == "pigeonpea") + PRCT_AREAvariety2crop2 *(CROPcrop2 == "pigeonpea")
    but this formula give missing value because PRCT_AREAvariety1crop2 and PRCT_AREAvariety2crop2 contain missing values. so remove this issue i used formula given below.

    egen pigeonpea_prct_area = rowtotal(PRCT_AREAvariety1crop1 *( CROPcrop1 == "pigeonpea") PRCT_AREAvariety1crop2 *(CROPcrop2 == "pigeonpea") PRCT_AREAvariety2crop2 *(CROPcrop2 == "pigeonpea"))

    but this formula resulted in error.
    factor-variable and time-series operators not allowed.

    i can not replace missing values in PRCT_AREAvariety1crop2 and PRCT_AREAvariety2crop2 to zero because my research question does not allow this.

    Please help me in resolving this issue.
    Thanks
    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 HH_ID int WAVE str4 PLOT_CO str9 SEASON str20(CROPcrop1 VAR_NAMEvariety1crop1) double PRCT_AREAvariety1crop1 str20(CROPcrop2 VAR_NAMEvariety1crop2) double PRCT_AREAvariety1crop2 str20 VAR_NAMEvariety2crop2 double PRCT_AREAvariety2crop2
    "IMPA0050" 2009 "AB" "Rabi"      "chickpea"      "KAJIA"           80 "mustard"    "LOCAL"             20 ""          .
    "IGJB0048" 2009 "B"  "Kharif"    "maize"         "LOCAL"           80 "pigeonpea"  "LOCAL"             20 ""          .
    "IKND0049" 2011 "D"  "Kharif"    "finger millet" "LOCAL"           80 "pigeonpea"  "LOCAL"             20 ""          .
    "IGJB0043" 2012 "AA" "Kharif"    "maize"         "LOCAL"           80 "pigeonpea"  "LOCAL"             20 ""          .
    "IKNC0047" 2013 "A"  "Kharif"    "pigeonpea"     "GULAL RED"       80 ""           ""                   . ""          .
    "IKND0055" 2009 "A"  "Kharif"    "finger millet" "LOCAL"           80 "pigeonpea"  "LOCAL"             10 ""          .
    "IMHB0205" 2011 "AC" "Kharif"    "blackgram"     "TAU-9"           80 "pigeonpea"  "MARUTI"            20 ""          .
    "IKND0047" 2013 "B"  "Perennial" "arecanut"      "LOCAL"           80 "betel vine" "LOCAL"             20 ""          .
    "IAPD0052" 2009 "A"  "Kharif"    "ladysfinger"   "N-49"            80 "pigeonpea"  "LRG-41"            20 ""          .
    "IKND0059" 2012 "AA" "Kharif"    "amaranthus"    "LOCAL"           80 "coriander"  "LOCAL"             20 ""          .
    "IGJB0035" 2011 "B"  "Kharif"    "maize"         "LOCAL"           80 "pigeonpea"  "HYBRID"            20 ""          .
    "IGJB0034" 2010 "AB" "Kharif"    "maize"         "LOCAL"           80 "pigeonpea"  "LOCAL"             20 ""          .
    "IKND0009" 2010 "A"  "Kharif"    "finger millet" "MR-2"            80 "pigeonpea"  "ASHA"              20 ""          .
    "IGJB0035" 2012 "B"  "Kharif"    "maize"         "LOCAL"           80 "pigeonpea"  "GT-1"              20 ""          .
    "IKND0042" 2010 "A"  "Kharif"    "finger millet" "MR-2"            80 "pigeonpea"  "MARUTHI"           20 ""          .
    "IKND0055" 2011 "AA" "Kharif"    "finger millet" "LOCAL"           80 "pigeonpea"  "LOCAL"             20 ""          .
    "IMHB0049" 2010 "AB" "Kharif"    "greengram"     "Kopargaon"       83 "pigeonpea"  "MARUTI"            17 ""          .
    "IMHB0059" 2010 "CB" "Kharif"    "greengram"     "NIRMAL-NAVAL"    83 "pigeonpea"  "MARUTI"            17 ""          .
    "IMHC0220" 2010 "D"  "Kharif"    "cotton"        "SUPER MAROTI"    83 "pigeonpea"  "MAROTI-ICRISAT"    17 ""          .
    "IKNC0052" 2009 "AA" "Rabi"      "chickpea"      "Annegiri"        83 "sorghum"    "M-35-1"            17 ""          .
    "IMHC0047" 2011 "AB" "Kharif"    "cotton"        "JAI"             83 "pigeonpea"  "Asha"              17 ""          .
    "IMHB0043" 2010 "AA" "Kharif"    "cotton"        "AK-5"            83 "pigeonpea"  "MARUTI"            17 ""          .
    "IMHB0219" 2010 "AB" "Kharif"    "cotton"        "DR.BRENT"        83 "pigeonpea"  "MAROTI"            17 ""          .
    "IMHB0224" 2010 "AB" "Kharif"    "greengram"     "DHANWAN"         83 "pigeonpea"  "DAFTARI"           17 ""          .
    "IMHC0220" 2009 "CA" "Kharif"    "cotton"        "BUNNY-2"       83.3 "pigeonpea"  "MAROTI"          16.7 ""          .
    "IMHC0055" 2009 "AB" "Kharif"    "cotton"        "SUPER MAROTI" 83.32 "pigeonpea"  "ICPL-88039"      8.34 "MAROTI" 8.34
    "IMHB0200" 2012 "AD" "Kharif"    "blackgram"     "TAU-1"        83.33 "pigeonpea"  "MARUTI"         16.67 ""          .
    "IMHB0050" 2009 "AB" "Kharif"    "cotton"        "AK-5"         83.33 "pigeonpea"  "MARUTI"         16.67 ""          .
    "IMHC0206" 2009 "AB" "Kharif"    "cotton"        "NHH-44"       83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHC0220" 2012 "D"  "Kharif"    "cotton"        "AH-5"         83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHC0221" 2013 "AB" "Kharif"    "cotton"        "JADDU"        83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHC0220" 2012 "F"  "Kharif"    "cotton"        "AH-5"         83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHC0221" 2013 "AA" "Kharif"    "cotton"        "AK-5"         83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHC0058" 2009 "BC" "Kharif"    "cotton"        "AK-5"         83.33 "pigeonpea"  "MAROTI"         16.67 ""          .
    "IMHB0203" 2012 "A"  "Kharif"    "greengram"     "NIRMAL NAVAL" 83.34 "pigeonpea"  "MARUTI"         16.66 ""          .
    "IMHB0200" 2012 "AC" "Kharif"    "greengram"     "NIRMAL NAVAL" 83.34 "pigeonpea"  "MARUTI"         16.66 ""          .
    end
    ------------------ copy up to and including the previous line ------------------

  • #2
    What confuses me about this data organization is why CROPcrop2 can come in 2 different varieties, but CROPcrop1 can only come in a single variety. So the combination of variety2crop1 is missing from the data layout. Is that intentional? Or did you just not show a complete set of variables and it got omitted?

    Comment


    • #3
      Dear Clyde Schechter, in my data set, there are maximum 6 crops in a plot and one crop have maximum 4 varieties. so in this data example set of variables are not complete and it got ommitted. Thanks

      Comment


      • #4
        OK. The problem is made difficult only because you have the data in double-wide layout. To do this, it is best if the data is long on crop. (It can be made double long on crop and variety, but that doesn't materially simplify the task, and the double-reshape required is a bit finicky to code properly.)

        Code:
        gen long obs_no = _n
        rename *crop? *_crop?
        ds *_crop2
        local stubs `r(varlist)'
        local stubs: subinstr local stubs "_crop2" "", all
        
        reshape long `stubs', i(obs_no) j(crop_num) string
        egen crop_area = rowtotal(PRCT_AREA*)
        by obs_no, sort: egen pigeonpea_pct_area = total(cond(CROP == "pigeonpea", crop_area, .))
        
        //  AND IF YOU HAVE GOOD REASON TO RETURN TO WIDE LAYOUT
        drop crop_area
        reshape wide
        I don't know where you are going with this data. If there is going to be more aggregation of data within crops, then you should skip the final two commands and just keep the data in the semi-long layout until all of that is done.

        Comment

        Working...
        X