Announcement

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

  • Average value in long format and delete the duplicate to make it only 1

    How can I find the average of total_fat and mono_fat if I have data in long format and some ID appears 1 time and some appears two times. I want to have only 1 ID if the ID appears twice, I want to average it and make it appears only 1 time.


    input str8 pid double(TOTAL_FAT MONO_FAT)
    "01010001" 38.980000000000004 12.6
    "01010001" 36.08 13.27
    "01010006" 70.07000000000001 25.23
    "01010006" 28.830000000000002 11.52
    "01010007" 38.34 13.76

    I want
    01010001 37.5 12.9
    01010006 39. 18
    0101007 38.3. 13.76

  • #2
    Like this? I got a different mean value of TOTAL_FAT for pid 01010006.
    Code:
    . collapse (mean) TOTAL_FAT MONO_FAT, by(pid)
    
    . list, abbrev(20) clean
    
                pid   TOTAL_FAT   MONO_FAT  
      1.   01010001       37.53     12.935  
      2.   01010006       49.45     18.375  
      3.   01010007       38.34      13.76
    David Radwin
    Senior Researcher, California Competes
    californiacompetes.org
    Pronouns: He/Him

    Comment

    Working...
    X