Announcement

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

  • How to calculate row mean

    Dear all,

    I need to create a new row according to one condition.

    My data look like this:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    * dataex ID I1 I2 I3 
    clear
    input str61 ID double(I1 I2 I3)
    "A" -.2512 -.2951 -.2552  
    "B" -.2757 -.3962 -.6286  
    "C"  -.3005 -.3327 -.2896  
    "D" -.1695  .0448 -.1167 
    "E" -.4392 -.6512 -.7259  
    end
    What I need is a new row called "F" for instance as mean between "D" and "E".

    Thank you in advance for your help.

    Best,
    Stefano



  • #2
    There must be a neat code for that, but you can use - egen - with rowmean() for D and E, then generate the mean value if str61 is equal to "D" or "E".
    Best regards,

    Marcos

    Comment


    • #3
      Code:
      tempfile copy
      save `copy'
      
      keep if inlist(ID, "D", "E")
      collapse (mean) I1 I2 I3
      gen ID = "F"
      append using `copy'
      sort ID
      list, noobs clean
      By the way, in Stata we do not speak of "rows" and "columns." They are referred to as "observations" and "variables," respectively. Stata is not a spreadsheet; rows and columns are spreadsheet concepts. If you speak about Stata in spreadsheet terms, you will tend to think about Stata as if it were a spreadsheet, and that will make it harder for you to use Stata effectively.

      Actually, as I think about it, what you are trying to do here, add a new observation that is the mean of existing observations, is a very spreadsheet-like thing to do and is very un-Stata-ish. In fact, unless all you are doing is preparing the data for a visual display, you are probably going to really mess up future analyses by doing this. The circumstances where a Stata data set is actually improved by having an observation that summarizes the information in other observations are extremely few and far between.
      Last edited by Clyde Schechter; 09 Sep 2020, 12:44.

      Comment


      • #4
        There many solutions to this task, and there are many critiques of this task, here
        https://www.statalist.org/forums/for...in-the-dataset

        Comment


        • #5
          Thank you all for your help!

          Comment


          • #6
            Clyde Schechter thank you for your valuable suggestions.

            Comment

            Working...
            X