I have a DF that looks like this:
V1 V2 V3 V4 V5 V6
1251 V5 12 7 13 91
126 V5 17 9 75 90
912 V6 55 34 88 22
I'm trying to get the value of the column referenced in V2.
For row 1 and row 2, column V2 references V5. For row 3, V2 references V6
Using dplyr I'm trying to get the corrosponding values of the column referenced in V2.
df %>%
mutate(V2_ref_value = get(V2)) %>%
select(V1, V2, V2_ref_value)
This returns an odd df:
V1 V2 V2_ref_value
1251 V5 13
126 V5 75
912 V6 88
The correct values for rows 1 and 2 show, but row 3 also shows its value from column V5, and not the value from its referenced column, V6.
The correct output should be:
V1 V2 V2_ref_value
1251 V5 13
126 V5 75
912 V6 22
Does anyone know why this is happening or how I can fix this?