Announcement

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

  • How to calculate the sum of different row and column

    Here is my dataset below , but I am confused about aggregating the"bold" value
    Do you have some methods to solve that? Thanks!

    id var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12
    1 92 232 0 428 92 950 3123 2427 91 591 654 1211
    2 40 132 0 219 16 1481 2905 2730 15 468 469 781
    3 4 138 2 154 149 1108 4235 3155 111 152 100 518
    4 86 617 1 820 128 1118 5681 3735 1 141 115 213

  • #2
    What is the rule underlying this calculation?

    Comment


    • #3
      I have three groups (var1-var4),(var5-var8),(var9-var12) and each group has four same elements such as A,B,C,D
      In this case I want to aggregate the id1 A element(which is called var1) + id4 A element (which is called var5) +id3 A element (which is called var9)

      Similarly, I would like to aggregate the id1 B element(which is called var2) + id4 B element (which is called var6) + id3 B element (which is called var10) and so on.
      Last edited by Stanly Lin; 31 Aug 2023, 10:41.

      Comment


      • #4
        Well, you have clearly explained how to pick out which variables ("columns") you want. But it is still a mystery why you picked rows 1, 4, and 3 to use for your A aggregation. Despite the fact that your post title emphasizes different rows and columns, I will ignore that and assume that you want to choose items all in the same observation ("row") and to do this for every observation in your data set.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(id var1) int var2 byte var3 int(var4 var5 var6 var7 var8 var9 var10 var11 var12)
        1 92 232 0 428  92  950 3123 2427  91 591 654 1211
        2 40 132 0 219  16 1481 2905 2730  15 468 469  781
        3  4 138 2 154 149 1108 4235 3155 111 152 100  518
        4 86 617 1 820 128 1118 5681 3735   1 141 115  213
        end
        
        egen aggregateA = rowtotal(var1 var5 var9)
        egen aggregateB = rowtotal(var2 var6 var10)
        egen aggregateC = rowtotal(var3 var7 var11)
        egen aggregateD = rowtotal(var4 var8 var12)
        In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Comment

        Working...
        X