Suppose I have a dataframe like the following:
X Y Z
1 b 3
2 a 8
3 a 7
4 c 1
5 b 6
6 a 4
7 a 9
8 b 5
9 a 4
I want to create columns A and B, which are dummy variables for if the value of Z is above or below the median value of Z within Group Y. So the desired output would be the following:
X Y Z A B
1 b 3 0 1
2 a 8 1 0
3 a 7 0 0
4 c 1 0 0
5 b 6 1 0
6 a 4 0 1
7 a 9 1 0
8 b 5 0 0
9 a 4 0 1