Dear R Studio community,
I have a dataset that looks like this but is much larger
Drug Start HN Blood_date Results
10/10/2015 1234 01/10/2010 90
10/10/2015 1234 01/10/2011 80
10/10/2015 1234 01/10/2012 70
10/10/2015 1234 01/10/2013 60
10/10/2015 1234 01/10/2014 50
10/10/2015 1234 01/10/2015 40
10/10/2015 1234 01/10/2016 30
10/10/2015 1234 01/10/2017 20
10/10/2015 1234 01/10/2018 19
10/10/2015 1234 01/10/2019 19
10/10/2015 1234 01/10/2020 18
10/10/2015 1234 01/10/2021 17
NA 4567 01/10/2010 90
NA 4567 01/10/2011 80
NA 4567 01/10/2012 70
NA 4567 01/10/2013 60
NA 4567 01/10/2014 50
NA 4567 01/10/2015 40
NA 4567 01/10/2016 30
NA 4567 01/10/2017 20
NA 4567 01/10/2018 19
NA 4567 01/10/2019 19
NA 4567 01/10/2020 18
I want to calculate the slope of the Results against time (Blood_date) but only for those dates that precede the "Drug Start" AND/OR prior to the Results being less than 20.In the first patient I would want to take the slope of GFRs prior to 10/10/2015 and in the second patient prior to 10/10/2017 when the Result becomes less than 20. The number of Results are not uniform per patient, not all patients have been started on the drug and not every patient's Results become <20. There are thousands of entries.
I can do simple line/slope calculations in R but the data munging part where it is conditional is challenging.
Your help would be greatly welcomed.