I have a data frame `dfA` with ~300 conditions and each condition has many rows containing diagnosis codes associated to them.

```
Condition <- as.character(c("COPD", "COPD", "COPD", "COPD", "HIV", "HIV", "HIV", "Sepsis", "Sepsis", "Sepsis", "Sepsis", "Sepsis"))
Code <- as.character(c("6A61.00", "8BPT.00", "8BPT000", "8BPT100", "E2E0.00", "E2E0100", "E2E0z00", "E2E1.00", "E2E2.00", "E2Ey.00", "E2Ez.00", "Eu84400"))
dfA <- data.frame(Condition, Code)
dfA
   Condition    Code
1       COPD 6A61.00
2       COPD 8BPT.00
3       COPD 8BPT000
4       COPD 8BPT100
5        HIV E2E0.00
6        HIV E2E0100
7        HIV E2E0z00
8     Sepsis E2E1.00
9     Sepsis E2E2.00
10    Sepsis E2Ey.00
11    Sepsis E2Ez.00
12    Sepsis Eu84400

```

I also have a data frame `dfB` with several health events per row. These events are identified with diagnosis codes.

```
Event <- as.double(1:12)
Code2 <- as.character(c("6A61.00", "Eu90z00", "8BPT000", "8BPT100", "Eu90111", "E2E0100", "E2E0z00", "E2E1.00", "E2E2.00", "Eu90z11", "Eu90z12", "Eu9y700"))
dfB <- data.frame(Event, Code2)
dfB
      Event   Code2
1      1 6A61.00
2      2 Eu90z00
3      3 8BPT000
4      4 8BPT100
5      5 Eu90111
6      6 E2E0100
7      7 E2E0z00
8      8 E2E1.00
9      9 E2E2.00
10    10 Eu90z11
11    11 Eu90z12
12    12 Eu9y700

```

I want to create a `column` in the `dfB` named after each **unique condition** in `dfA` (note that `dfA` has multiple rows for each unique condition) that identifies the presence of at least one diagnosis code present for each `Condition`. The idea is - if a row in `dfB` contains diagnosis codes present in the `Condition` `HIV`, for instance, then the `column` `HIV` in `dfB` will receive the value `1`, otherwise, `0`. For example:

```
dfB$"COPD" <- 0
dfB$"COPD"[which(dfB$Code2 %in% (dfA$Code[which(dfA$Condition== "COPD")]))] <- 1

dfB$"HIV" <- 0
dfB$"HIV"[which(dfB$Code2 %in% (dfA$Code[which(dfA$Condition== "HIV")]))] <- 1

dfB$"Sepsis" <- 0
dfB$"Sepsis"[which(dfB$Code2 %in% (dfA$Code[which(dfA$Condition== "Sepsis")]))] <- 1

dfB
      Event   Code2 COPD HIV Sepsis
1      1 6A61.00    1   0      0
2      2 Eu90z00    0   0      0
3      3 8BPT000    1   0      0
4      4 8BPT100    1   0      0
5      5 Eu90111    0   0      0
6      6 E2E0100    0   1      0
7      7 E2E0z00    0   1      0
8      8 E2E1.00    0   0      1
9      9 E2E2.00    0   0      1
10    10 Eu90z11    0   0      0
11    11 Eu90z12    0   0      0
12    12 Eu9y700    0   0      0

```

I expect to have created in the `dfB` one `column` for each unique `Condition` present in `dfA`. However, I don't want to have to create a `column` for each condition individually, as I have 300 conditions. Is there a better way of optimizing a piece of code to create the 300 `columns` in my `dfB` for each unique `Condition` from `dfA` at once? The `columns` need to be named after the `Conditions`.

I appreciate your help very much!