I am trying to solve a problem in R that seems simple, but I can't work it out.
I have the following data:
tmp
town_id city_ id flag
1 10500 111 1
2 15300 1110 1
3 9400 11100 1
4 9400 11101 0
5 9600 11102 0
6 9800 11103 0
There is a duplicate town_id, and I would like to remove it while assigning the highest value in flag. That is, I would like to have:
town_id city_ id flag
1 10500 111 1
2 15300 1110 1
3 9400 11100 1
4 9600 11102 0
5 9800 11103 0
I tried to use the following dplyr code, but it assigns a 1 to everything:
tmp_2<-tmp %>% group_by(town_id) %>% mutate(flag=max(flag))
tmp_2
town_id city_ id flag
1 10500 111 1
2 15300 1110 1
3 9400 11100 1
4 9400 11101 1
5 9600 11102 1
6 9800 11103 1
Could someone please tell me what I am doing wrong?
Thank you.