I have a dataframe df1 of patients with numeric codes for adverse medical events based on columns Class and Subclass of the adverse event. A subclass code (eg 3) can be represent different things depending on the Class (Class 4 subclass 3 is different than Class 5 subclass 3).
ID = c(2, 2, 2, 2, 3, 5)
Class = c(4,4,4,5,5,5)
Subclass = c(1,2,3,1,2,3)
df1 = data.frame(ID, Class, Subclass)
ID Class Subclass
1 2 4 1
2 2 4 2
3 2 4 3
4 2 5 1
5 3 5 2
6 5 5 3
Dataframe df2 is the code key for adverse events
Class = c(4,4,4,5,5,5)
Subclass = c(1,2,3,1,2,3)
Class.description = c("Heart", "Heart", "Heart", "Lung", "Lung", "Lung")
Subclass.description = c("pericarditis", "myocarditis", "endocarditis", "asthma", "pneumonia", "COPD")
df2 = data.frame(Class, Subclass, Class.description, Subclass.description)
df2
Class Subclass Class.description Subclass.description
1 4 1 Heart pericarditis
2 4 2 Heart myocarditis
3 4 3 Heart endocarditis
4 5 1 Lung asthma
5 5 2 Lung pneumonia
6 5 3 Lung COPD
I would like to match the dataframes such that df1 looks like below:
ID Class Subclass
1 2 Heart pericarditis
2 2 Heart myocarditis
3 2 Heart endocarditis
4 2 Lung asthma
5 3 Lung pneumonia
6 5 Lung COPD
Any advice?