Announcement

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

  • How many in group are named the same

    Hello,
    I have data of groups and individuals. Each individuals is assigned a name. For each individual I want to know how many more individuals are named like him/her within the group.

    So my data looks like the first three colums (Id, Group, Name) and I want to add the 4th column (SHARE_NAMES):
    Id Group Name SHARE_NAMES
    1 1 JACK 2
    2 1 JACK 2
    3 1 ANNA 0
    4 1 JACK 2
    5 1 MICHAEL 0
    6 2 JACK 0
    7 2 NICOLE 1
    8 2 MICHAEL 0
    9 2 NICOLE 1

    Thank you very much for any help.

  • #2
    Under the aegis of by: the number of observations in any defined group is _N. So the number of others is one fewer.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id group) str7 name byte share_names
    1 1 "JACK"    2
    2 1 "JACK"    2
    3 1 "ANNA"    0
    4 1 "JACK"    2
    5 1 "MICHAEL" 0
    6 2 "JACK"    0
    7 2 "NICOLE"  1
    8 2 "MICHAEL" 0
    9 2 "NICOLE"  1
    end
    
    bysort group name : gen wanted = _N - 1
    
    list, sepby(group name)
    
         +------------------------------------------+
         | id   group      name   share_~s   wanted |
         |------------------------------------------|
      1. |  3       1      ANNA          0        0 |
         |------------------------------------------|
      2. |  2       1      JACK          2        2 |
      3. |  1       1      JACK          2        2 |
      4. |  4       1      JACK          2        2 |
         |------------------------------------------|
      5. |  5       1   MICHAEL          0        0 |
         |------------------------------------------|
      6. |  6       2      JACK          0        0 |
         |------------------------------------------|
      7. |  8       2   MICHAEL          0        0 |
         |------------------------------------------|
      8. |  9       2    NICOLE          1        1 |
      9. |  7       2    NICOLE          1        1 |
         +------------------------------------------+
    See also https://www.stata-journal.com/articl...article=dm0075
    Last edited by Nick Cox; 30 Nov 2022, 19:07.

    Comment

    Working...
    X