I have a reference dataframe df which determines the relationship between a number code and the name associated to it.
df <- data.frame(Number = 1:5,
Name = c("Aaa", "Bbb", "Ccc", "Ddd", "Eee"))
Now, I have a vector of numbers number <- c(1,1,3,5,6) and I want to get their corresponding Name, taking into account that:
- There are repeated numbers
- Some of the numbers may not have an associated name in the table
So, the result I expect is:
df2 <- data.frame(number,
Name = c("Aaa", "Aaa", "Ccc", "Eee", NA))
The vector of numbers I have is very long, any idea about how to do it?
>Solution :
> df$Name[match(number,df$Number)]
[1] "Aaa" "Aaa" "Ccc" "Eee" NA