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

R join a table column from a vector

I would like to get the corresponding values of a vector in a table from a column in another column. (just look below)

example:

Vector:

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

v = c('A', 'B', 'C')

Table :

# key     Value
 'C'        3
 'A'        1
 'B'        2

When I give the vector v (A, B, C) I want to get back the corresponding values in the good order 1, 2, 3.

In reality, the vector is the rownames of a dataset, and I need to replace it with the corresponding values.

I was thinking about using the left_join function from Dplyr but I would need 2 tables for this.

Thanks for your help

>Solution :

A possible solution in base R:

df$Value[match(v, df$key)]

#> [1] 1 2 3

Using dplyr:

library(dplyr)

df %>% 
  mutate(x = Value[match(v, key)]) %>% 
  pull(x)

#> [1] 1 2 3
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