I have data where I have multiple observations for each ID. I would like to aggregate/collapse the data by ID, however, some of the additional columns have discrepancies (the same ID could be male for one observation and female for another). If all observations agree, I'd like to keep the label, if they're are discrepancies, I'd like to replace it with "missing".
This is what I have
ID <- c("1234", "1234", "1242", "1456", "1234", "1242", "1234", "1234")
MEMBER_GENDER <- c("M", "M","F", "F", "M", "M", "M", "M")
RELATIONSHIP_TYPE_CODE <- c("1","1", "2", "3", "3", "2", "4" ,"3")
data <- data.frame(cbind(ID,MEMBER_GENDER, RELATIONSHIP_TYPE_CODE))
ID MEMBER_GENDER RELATIONSHIP_TYPE_CODE
1 1234 M 1
2 1234 M 1
3 1242 F 2
4 1456 F 3
5 1234 M 3
6 1242 M 2
7 1234 M 4
8 1234 M 3
This is what I'd like
ID2 <- c("1234", "1242", "1456")
MEMBER_GENDER2 <- c("M", "missing", "F")
RELATIONSHIP_TYPE_CODE2 <- c("missing", "2", "3")
data2 <- data.frame(cbind(ID,MEMBER_GENDER, RELATIONSHIP_TYPE_CODE))
ID MEMBER_GENDER RELATIONSHIP_TYPE_CODE
1 1234 M missing
2 1242 missing 2
3 1456 F 3
I can aggregate the data based on unique values, but then I end up with this
aggregate(.~ID, data=data, FUN=unique)
ID MEMBER_GENDER RELATIONSHIP_TYPE_CODE
1 1234 M 1, 3, 4
2 1242 F, M 2
3 1456 F 3
I'm not sure how to fill with missing instead of a combination of the variables.