Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the associated values of a vector in a dataframe

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading