Consider the following data frame
address <- c('9A Eagle Point N','9A Eagle Point N','9A Eagle Point N', '9999 Mineral Wells Highway', '9999 Mineral Wells Highway')
sale_status <- c('Succeeded', 'Failed', 'Failed', 'Failed', 'Failed')
sale_date <- as.Date(c('2020-03-01','2020-02-01', '2020-01-14', '2020-03-02', '2019-08-01'))
df = data.frame(address, sale_status, sale_date)
such that the data looks like this:
1 9A Eagle Point N Succeeded 2020-03-01
2 9A Eagle Point N Failed 2020-02-01
3 9A Eagle Point N Failed 2020-01-14
4 9999 Mineral Wells Highway Failed 2020-03-02
5 9999 Mineral Wells Highway Failed 2019-08-01
I am trying to write code such that for any number of n rows with matching addresses, that the earlier matching (duplicated) rows are removed whenever the latest row was successfully sold within 180 days of the second-to-most-recent sale's date. I only want the previous matching rows to be removed when the latest rows have a sale_status
of "Succeeded (df$sale_status == "Succeeded
) and the earlier matching rows have a sale_status
of "Failed" (df$sale_status == "Failed"
)
I know this sounds incredibly convoluted, but any assistance would be greatly appreciated. I checked out several other posted questions but none seemed to address this use case.
I believe the resulting data frame would look like this:
1 9A Eagle Point N Succeeded 2020-03-01
4 9999 Mineral Wells Highway Failed 2020-03-02
5 9999 Mineral Wells Highway Failed 2019-08-01
rows are removed whenever the latest row was successfully sold within 180 days of the second-to-most-recent sale's date
?Failed
rows, but leaveFailed
rows in if they happened more than 180 days prior to theSucceeded
date? If this is your intended logic, none of the dates in your example test this behavior.