I have a dataset that looks like this
data <- data.frame(ID = c("1a", "1b", "2a", "2b", "3a", "4b", "5a", "5b"),
Sex = c(1, 2, 2, 1, 1, 2, 1, 2))
ID Sex
1a 1
1b 2
2a 2
2b 1
3a 1
4b 2
5a 1
5b 2
I want to filter based on ID. Specifically, if there are same numbers in a string in ID, such as 1a and 1b, 2a and 2b, and 5a and 5b, then I want to filter rows with Sex = 1. Additionally, I want to keep the rows with 3a and 4b, because it does not have its counterparts of 3b and 4a, regardless of its value in Sex.
My final desired output is:
ID Sex
1a 1
2b 1
3a 1
4b 2
5a 1
Thank you for your help!