I did some search but cannot find an obvious answer of this question so hopefully it's not a duplicated question. I have a data frame looks like this:
X1 X2 V1 V2 V3 ... Vn
A B 0 1 2 1
B C 1 0 1 0
A C 2 1 0 1
What I want to achieve is to replace V1 to Vn values to the "dosage" of X2. So for row 1 (each row may have different values of X1 and X2),
- if the value is 0, I want to replace it to AA;
- if the value is 1, I want to replace it to AB;
- if the value is 2, I want to replace it to BB;
The expected outcome is:
X1 X2 V1 V2 V3 ... Vn
A B AA AB BB AB
B C BC BB BC BB
A C CC AC AA AC
Here is the sample data frame:
df=data.frame(X1=c("A","B","A"),
X2=c("B","C","C"),
V1=c(0,1,2),
V2=c(1,0,1),
V3=c(2,1,0))
Thanks for the help!