I am trying to create a new table where table1 is grouped by ID and only the most recent date is picked from table1$new.
I have the tables below:
1st Table:
| ID | Status | Date |
|---|---|---|
| 001 | 1003 | 2021-02-01 |
| 001 | 1003 | 2021-02-01 |
| 001 | 1003 | 2021-02-21 |
| 001 | 1003 | 2021-04-21 |
| 002 | 1003 | 2021-05-01 |
| 002 | 1003 | 2021-05-01 |
| 002 | 1003 | 2021-05-21 |
| 002 | 1003 | 2021-07-21 |
| 003 | 1004 | 2021-05-01 |
| 003 | 1004 | 2021-05-01 |
| 003 | 1004 | 2021-05-21 |
| 003 | 1004 | 2021-07-21 |
Current Output
| ID | Status | Date |
|---|---|---|
| 001 | 1003 | 2021-02-01 |
| 001 | 1003 | 2021-02-01 |
| 001 | 1003 | 2021-02-21 |
| 001 | 1003 | 2021-04-21 |
| 002 | 1003 | 2021-05-01 |
| 002 | 1003 | 2021-05-01 |
| 002 | 1003 | 2021-05-21 |
| 002 | 1003 | 2021-07-21 |
Desired Output:
| ID | New |
|---|---|
| 001 | 2021-4-21 |
| 002 | 2021-07-21 |
Here is my code as of now
desired <- table1 %>%
group_by(ID) %>%
subset(Status == '1003', select = c("ID", "Date")) %>% rename(New = Date)
I think my first mistake is in the group_by and I am not sure how to only keep the most recent date.