I am trying to figure out a way to code in R how to join two dataframes to obtain the following:
The time diference between the *FIRST bad report and the first call a teacher takes to call each student´s parent AFTER that first bad report is recorded
make a count of the numbers of calls a teacher has called the parents of a student AFTER the FIRST bad report for each student.
To Ilustrate here are the tables:
DATE_REPORT | STUDENT_ID | REPORT_CODE |
---|---|---|
2021-02-23 10:53:34.565 | H-007 | UDJ-83 |
2021-02-25 17:02:29.99 | H-007 | UDJ-84 |
2021-02-28 17:02:29.100 | H-008 | UDJ-85 |
BAD_REPORTS<-data.frame(
"DATE_REPORT"=c("2021-02-23 10:53:34.565",
"2021-02-25 17:02:29.99",
"2021-02-28 17:02:29.100"),
"STUDENT_ID"=c("H-007",
"H-007",
"H-008"),
"REPORT_CODE"=c("UDJ-83",
"UDJ-84","UDJ-85"))
This table shows the dates of the bad report for FEB by STUDENT_ID
the following table shows the records of the calls the teacher has made the same month
DATE_CAL | STUDENT_ID |
---|---|
2021-02-10 12:21:07 | H-007 |
2021-02-23 10:26:08 | H-007 |
2021-02-26 11:09:36 | H-007 |
2021-02-26 11:16:46 | H-007 |
2021-02-27 11:16:46 | H-008 |
CALLS_BY_TEACHER<-data.frame(
"DATE_CAL"=c("2021-02-10 12:21:07",
"2021-02-23 10:26:08",
"2021-02-26 11:09:36",
"2021-02-26 11:16:46",
"2021-02-27 11:16:46"),
"STUDENT_ID"=c("H-007",
"H-007",
"H-007",
"H-007",
"H-008"))
Im looking for a table like this:
STUDENT_ID | DATE_FIRST_REPORT | FIRST_CALL_AFTER_FIRST_REPORT | DIF_TIME | REPORT_CODE |
---|---|---|---|---|
H-007 | 2021-02-23 10:53:34.565 | 2021-02-26 11:09:36 | 4336.024 | UDJ-83 |
H-007 | 2021-02-28 17:02:29.100 | NA | NA | UDJ-85 |
This would be correct because AFTER the first report of this month the teacher called student H-007 on 2021-02-26 11:09:36 (4336.024 mins after the first bad report)
what am I doing?
I have tried to do a full join and then group by STUDENT_ID and REPORT and I select the min(DATE_REPORT) because I know that it has to be the very first date of report but I am kind of lost when I have to code that the first date of call to be taken into consideration will have to be the one RIGHT AFTER the date of the first bad report for each student... so if someone could link some documentation that would be great THANK YOU SO MUCH FOR YOUR HELP
sidenote: If there is no call on the teacher's behalf after a student's frist report than the diff time would be NA