I have a data set similar to the example below. What I want to be able to do is assign all missing values the correct 'Title' value, based on the non-missing values and their associated 'Name'. So all entries with Name 'A' would have Title 'X', and similar for B and 'Y'.
Name | Title
-------------
A | X
A | NA
A | NA
B | NA
B | Y
B | Y
There should only be a single 'Title' value for each 'Name', although this one value may appear multiple times.
I imagine there are several tortuous conditional loops that could achieve this, but I'm curious if there are any tidier/more efficient ways to go about this?
There might be more elegant solutions, but this is pretty straightforward and should be fairly robust:
lu <- unique(df[complete.cases(df),]) ## Make a look-up table
df$Title <- lu$Title[match(df$Name, lu$Name)] ## Use it to find Name-->Title mappings
## Check that it worked
df
# Name Title
# 1 A X
# 2 A X
# 3 A X
# 4 B Y
# 5 B Y
# 6 B Y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With