I have a dataframe as follows:
ID | Col1 | RespID | Col3 | Col4 |
---|---|---|---|---|
1 | blue | 729Ad | 3.2 | A |
2 | orange | 295gS | 6.5 | A |
3 | red | 729Ad | 8.4 | B |
4 | yellow | 592Jd | 2.9 | A |
5 | green | 937sa | 3.5 | B |
I would like to calculate a new column, Col5, such that its value is 1 if the row has Col4 value of A and there exists another column somewhere in the dataset a row with the same RespId but a Col4 value of B. Otherwise it’s value is 0. Then I will drop all rows with Col4 value of B, to keep just those with A. I would like to do this using the R tidymodels recipe package. I’d also like to do this all with data frames.
Here is what the desired output table would look like prior to dropping rows with Col4 value of B:
ID | Col1 | RespID | Col3 | Col4 | Col5 |
---|---|---|---|---|---|
1 | blue | 729Ad | 3.2 | A | 1 |
2 | orange | 295gS | 6.5 | A | 0 |
3 | red | 729Ad | 8.4 | B | 0 |
4 | yellow | 592Jd | 2.9 | A | 0 |
5 | green | 937sa | 3.5 | B | 0 |