Announcement

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

  • calculating average

    I understand this is a simple question. I am trying to calculate average age on the basis of Country and gender. When i run this command it creates another variable in the variable list with name avg_age
    egen avg_age = mean(Age), by(Country gender)

    Can someone guide how can I get it in a table form
    Country Gender Avg_age
    Kor Male 70
    Kor Female 80
    etc

  • #2
    I can't get you that exact layout, but here's something close:

    Code:
    table Country Gender, c(mean Age) format(%2.1f)
    Note: The -format(%2.1f)- option is just my guess at what's appropriate for your data. Modify accordingly.

    Comment


    • #3
      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . egen mean = mean(mpg), by(foreign rep78)
      
      . format mean %2.1f
      
      . * ssc inst groups is needed for this to run
      
      . groups foreign rep78 mean, show(none)
      
        +-------------------------+
        |  foreign   rep78   mean |
        |-------------------------|
        | Domestic       1   21.0 |
        | Domestic       2   19.1 |
        | Domestic       3   19.0 |
        | Domestic       4   18.4 |
        | Domestic       5   32.0 |
        |-------------------------|
        |  Foreign       3   23.3 |
        |  Foreign       4   24.9 |
        |  Foreign       5   26.3 |
        +-------------------------+
      show(none) just means that there is no display of frequencies, percents, etc. The separator lines appear by default and here fortuituously but fortunately separate the first and second groups. They too can be suppressed. groups is just a wrapper for list.
      Last edited by Nick Cox; 07 Mar 2017, 02:18.

      Comment

      Working...
      X