For data on individuals in groups choosing items, at the individual by "set of possible items" level, I want to count the number of other group members who chose that item. For example, data on students in class groups who are choosing books:
So ideally num_classmates would contain, for each id-book, how many of id's classmates chose that book.
I'm not sure how to do this without something very inefficient like looping through each observation. The attempt above was:
which seems to be correct for the id-choice actually chosen (i.e. when possible_book == book), but incorrect for the other possible choices. In the above example, person 1 chose book A, and has two classmates (persons 2 and 3) who also chose A. But person 1 has one classmate who chose B (person 4), and no classmates who chose C. Ideally this could be extended to condition on other attributes of the person as well, such as number of classmates who are male, etc.
I found it difficult to know what to search for this question, so any guidance would be appreciated!
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(id class) str1(possible_book book) float num_classmates 1 10 "A" "A" 2 1 10 "B" "A" 2 1 10 "C" "A" 2 2 10 "A" "A" 2 2 10 "B" "A" 2 2 10 "C" "A" 2 3 10 "A" "A" 2 3 10 "B" "A" 2 3 10 "C" "A" 2 4 10 "A" "B" 0 4 10 "B" "B" 0 4 10 "C" "B" 0 5 20 "A" "B" 1 5 20 "B" "B" 1 5 20 "C" "B" 1 6 20 "A" "C" 3 6 20 "B" "C" 3 6 20 "C" "C" 3 7 20 "A" "C" 3 7 20 "B" "C" 3 7 20 "C" "C" 3 8 20 "A" "C" 3 8 20 "B" "C" 3 8 20 "C" "C" 3 9 20 "A" "C" 3 9 20 "B" "C" 3 9 20 "C" "C" 3 10 20 "A" "B" 1 10 20 "B" "B" 1 10 20 "C" "B" 1 end
I'm not sure how to do this without something very inefficient like looping through each observation. The attempt above was:
Code:
bysort class possible_book book: egen num = count(id) replace num = num - 1 // Don't count self as a classmate
I found it difficult to know what to search for this question, so any guidance would be appreciated!
Comment