Dear all,
I am trying to create a variable which is the mean of another variable of all observation but the current one in the same location. In the data I have the following variables; observation_id, location_id, and rate, which are all numeric. In the data below for example, I want to create n_rate(label as neighbours' rate) i.e. I want the average rate of neighbours assuming that observations with the same location id are neighbours.
gives me the mean of all observations in each location but I want to exclude current observations in each case.
I would be grateful for assistance.
Thank you.
I am trying to create a variable which is the mean of another variable of all observation but the current one in the same location. In the data I have the following variables; observation_id, location_id, and rate, which are all numeric. In the data below for example, I want to create n_rate(label as neighbours' rate) i.e. I want the average rate of neighbours assuming that observations with the same location id are neighbours.
Observation_id | Location_id | rate | n_rate |
1 | 1 | 5 | 4.5 |
2 | 1 | 6 | 4 |
3 | 1 | 3 | 5.5 |
4 | 2 | 2 | 4.5 |
5 | 2 | 4 | 3.5 |
6 | 2 | 5 | 3 |
7 | 3 | 7 | 2.75 |
8 | 3 | 3 | 3.75 |
9 | 3 | 5 | |
10 | 3 | 2 | |
11 | 3 | 1 | |
12 | 4 | 3 | |
13 | 4 | 4 | |
14 | 5 | 2 | |
15 | 5 | 6 | |
16 | 5 | 3 | |
17 | 5 | 5 | |
18 | 6 | 4 | |
19 | 6 | 1 |
Code:
bys location_id: egen Neighbours_rate=mean(rate)
I would be grateful for assistance.
Thank you.