Conditionally replace matching values from one data.frame to values in another data.frame

I have a df1 that looks like

[df1]

DE201 DE207
A15 A903
A18 A906

...

df1 <- data.frame(DE201 = c("A15", "A18"),
                  DE207 = c("A903", "A906"))

and another df2 that has the dictionary values for those values, row wise

[df2]

module Data_Element_ID Data_Element_Name Answer_Code Answer_Value DE_original
Fall DE201 Injury result A15 Yes DE201_A15
Fall DE201 Injury result A18 No DE201_A18
Fall DE207 Patient activity A903 Ambulating with assistance DE207_A903
Fall DE207 Patient activity A906 Intracranial injury‬ DE207_A906

...

df2 <- data.frame(module = rep("Fall", 4),
                  Data_Element_ID = c(rep("DE201", 2), rep("DE207", 2)),
                  Data_Element_Name = c(rep("Injury result", 2), rep("Patient activity", 2)),
                  Answer_Code = c("A15", "A18", "A903", "A906"),
                  Answer_value = c("Yes", "No", "Ambulating with assistance", "Interracial injury"),
                  DE_original = c("DE201_A15", "DE201_A18", "DE207_A903", "DE207_A906")
                  )

I want to replace all the values in df1, including its column name, to be the matching values in df2 (df1 column names to be matched df2's Data_Element_Name, and df1 values to be matched df2's Answer_Value)

for example, my desired output is something like

Injury result Patient activity
Yes Ambulating with assistance
No Intracranial injury‬

...

output <- data.frame(Injury_result = c("Yes", "No"),
                    Patient_activity = c("Ambulating with assistance", "Interracial injury"))

I've tried below two methods but none of them worked, and merge/join are not really applicable here because they don't really have the same key/ID to join/merge by, since one is column wise elements , and one is row wise elements. Tried mutate with if else and case_when:

mutate(
    DE201 = case_when(DE201 == df2$Answer_Code, ~df2$Answer_Value, 
                      TRUE ~DE201
                     ),
    DE204 = ifelse(DE204 %in% df2$Answer_Code, df2$Answer_Value, DE204)
)


from Recent Questions - Stack Overflow https://ift.tt/3ff9SmM
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)