Announcement

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

  • Multiple modes

    Hi everyone,

    I have a dataset that looks like below. For each of the IDs, there are multiple locations. What I want to do is to replace the location for ids with one mode to the value of the mode. For the ids that have multiple modes of locations, I'll keep the location as it is. In addition, I want to know how many unique ids have multiple modes for location.

    I wonder if anyone know how to realize that in stata? I was using the command
    Code:
     
     bysort id (location):egen mode = mode(location)


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id str1 location
    1 "A"
    1 "A"
    1 "A"
    1 "B"
    2 "C"
    2 "C"
    2 "C"
    2 "D"
    2 "D"
    2 "D"
    3 "E"
    3 "E"
    3 "E"
    3 "F"
    3 "F"
    3 "F"
    end
    Thank you so much!


  • #2
    Code:
    by id location, sort: gen location_freq = _N
    by id (location_freq location), sort: egen n_modes = ///
        total(location != location[_n-1] & location_freq == location_freq[_N])
    by id (location_freq location): replace location = location[_N] if n_modes == 1

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      by id location, sort: gen location_freq = _N
      by id (location_freq location), sort: egen n_modes = ///
      total(location != location[_n-1] & location_freq == location_freq[_N])
      by id (location_freq location): replace location = location[_N] if n_modes == 1
      Hi Clyde,

      The code works perfectly for me. It helps a lot! Thank you so much for your reply.

      Comment

      Working...
      X