Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - How to conditionally set multiple values in a vector based on a single current value

Tags:

r

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?

like image 969
E Keith Avatar asked Dec 25 '22 13:12

E Keith


1 Answers

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
like image 167
Josh O'Brien Avatar answered Feb 13 '23 04:02

Josh O'Brien