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:
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"