I Have a data set that looks like this: input data
I need R to take create a new column "Ref" and choose between Allele_A & Allele_B based on the value of Hom_Ref_A.
If Hom_Ref_A = 2 it should be choosing from Allele_A, and if Hom_Ref_A = 0 it should choose from Allele_B and write these to the Ref column
and the reverse for an "Alt" column (Allele_A if Hom_Ref_A = 0, and Allele_B if Hom_Ref_A = 2)
The output should look like this: result
I would really appreciate any help on this
CodePudding user response:
With case_when:
df %>%
mutate(
Ref = case_when(
Hom_Ref_A == 2 ~ Allele_A,
Hom_Ref_A == 0 ~ Allele_B
),
Alt = case_when(
HomRef_A == 2 ~ Allele_B,
HomRef_A == 0 ~ Allele_A
)
)
