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

Replacing values of a vector with value of a dataframe column based on a reference in R

I am trying to do a simple task under normal circumstances, however, in this case, I’m dealing with large data with a different format (snp.data, GENABEL package). So the challenge here is not the task itself but managing to do it in a single-line code without further data manipulation.

So I’m trying to change a column (that acts like a vector during manipulation) with different variables in a different data frame. The very same data frame also has a reference column to match the variables that will be changed.

Let me explain with a reproducible example:

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

vec = c("424","425","426","429", "430", "455","467","468")

df = data.frame(ID = c("426","429", "430","424","425","455","467","468", "508","601"),
               ID_rep = c("D1","D2", "D3","D4","D5","D6","D7","D8","D9","D10"))

So vec should be changed with the ID_rep column in df based on the matching information of the ID column (reference) in df again.

In other words vec should go from this:

> vec
[1] "424" "425" "426" "429" "430" "455" "467" "468"

to this:

> desired.result
[1] "D4" "D5" "D1" "D2" "D3" "D6" "D7" "D8"

Again, this is going to change a vector like a column in meta data, so I don’t think multi-step data manipulation ways aren’t feasible since that part of the data has so many different structures and uneven sizes.

Thanks in advance!

>Solution :

Something like this?

desired.result <- df$ID_rep[match(vec, df$ID)]

output

[1] "D4" "D5" "D1" "D2" "D3" "D6" "D7" "D8"
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