0

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?

gfa2001
  • 227
  • 1
  • 2
  • 10

1 Answers1

0
df_new = merge(df1,df2,by=c("Class","Subclass"));
MSW Data
  • 441
  • 3
  • 8
  • 2
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 11 '18 at 05:49