I have a dummy dataframe with four columns.
df <- data.frame(City = c("A","A","A","B","B","B","B"),
Name=c("Jon", "Bill","Bill", "Maria", "Ben", "Tina",'Tina'),
Age = c(23, 41, 32, 58, 26, 12, 15),
Eye_color=c("Blue","Blue", "Brown", "Brown", "Blue", "Blue","Brown"))
City Name Age Eye_color
1 A Jon 23 Blue
2 A Bill 41 Blue
3 A Bill 32 Brown
4 B Maria 58 Brown
5 B Ben 26 Blue
6 B Tina 12 Blue
7 B Tina 15 Brown
I want to remove duplicates in Names (Bill and Tina) with two different cases:
First case: group by City and remove duplicates in Names keeping the Blue eyed only. Result 1 should look like this:
City Name Age Eye_color
1 A Jon 23 Blue
2 A Bill 41 Blue
3 B Maria 58 Brown
4 B Ben 26 Blue
5 B Tina 12 Blue
Second case: I want to specify that if the city is A, between the duplicates in Names keep Blue eye, if the City is B between the duplicates in Name keep the Brown eye.
Result 2 should look like this:
City Name Age Eye_color
1 A Jon 23 Blue
2 A Bill 41 Blue
3 B Maria 58 Brown
4 B Ben 26 Blue
5 B Tina 15 Brown
Thanks for the help!