I have a series of phone numbers that have individual names associated with them. The phone numbers are not necessarily unique, and sometimes the name associated with them is missing. I would like to fill in the names with the following rules:
- If a grouped phone number has blank entries and only a single name available, I would like to replace the blank names with the one available.
- If the grouped number has entries with multiple non-blank names, then I cannot determine who belongs to that phone number and so I make no changes.
Here is the original table:
library("dplyr")
phone = c("1234","1234","1234","4567","4567","4567","4567","4567");phone
sgroup = c("","Jones","","Smith","","Smith","Williams","Jones");sgroup
DATA = data.frame(phone,sgroup);DATA
And here is how I want it to look:
phone1 = c("1234","1234","1234","4567","4567","4567","4567","4567");phone1
sgroup1 = c("Jones","Jones","Jones","Smith","","Smith","Williams","Jones");sgroup1
DATA1 = data.frame(phone1,sgroup1)
Note the "1234" since it originally had only 'Jones" and Blanks(NAs) assigned, so I assigned all records in that group to 'Jones. The second number (4567) had multiple names assigned, so I was unable to determine a common name so no assignment was made.
I can determine which phones have blank or non-blank name assignments, but I have been unable to determine how to assign them back to the blank records in that group.
Countdf1 <- DATA %>% count(phone,sgroup)