0

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
3
  • Do you mean remove everything after "Succeeded" ? Can you also include in the example a case for rows are removed whenever the latest row was successfully sold within 180 days of the second-to-most-recent sale's date ?
    – Ronak Shah
    Commented Apr 14, 2020 at 1:22
  • For every row with matching addresses, I’m trying to remove the duplicated rows only when the most recent row has a sale status of “Succeeded” essentially If a property successfully sold, I want to remove the failed attempts before. Commented Apr 14, 2020 at 1:36
  • You want to remove Failed rows, but leave Failed rows in if they happened more than 180 days prior to the Succeeded date? If this is your intended logic, none of the dates in your example test this behavior. Commented Apr 14, 2020 at 1:57

2 Answers 2

2

Here's an approach with lag from dplyr.

First we calculate the difference in days between dates per address with lag. We then calculate the cumulative days into the past from the final succeeded date with cumsum. Then we filter rows that fulfill any of the following:

  1. Succeeded
  2. Never sold
  3. Failed more than 180 days before the sale succeeded
library(dplyr)
df %>%
  arrange(desc(sale_date)) %>%
  group_by(address) %>% 
  mutate(elapsed = sale_date - lag(sale_date,1L,default=first(sale_date)),
         cumelapse = cumsum(as.integer(elapsed)),
         sold = any(sale_status == "Succeeded")) %>%
  filter( sale_status == "Succeeded" | sold == FALSE | (sold == TRUE & cumelapse < -180)) %>%
  ungroup %>% arrange(address,sale_date)
#  address                    sale_status sale_date  elapsed   cumelapse sold 
#  <fct>                      <fct>       <date>     <drtn>        <int> <lgl>
#1 9999 Mineral Wells Highway Failed      2019-08-01 -214 days      -214 FALSE
#2 9999 Mineral Wells Highway Failed      2020-03-02    0 days         0 FALSE
#3 9A Eagle Point N           Succeeded   2020-03-01    0 days         0 TRUE 
2

We can check if any sale_status == 'Succeeded' in each address and select only rows which are greater than the corresponding sale_date.

library(dplyr)

df %>%
  group_by(address) %>%
  filter(if(any(sale_status == 'Succeeded')) 
        sale_date >= sale_date[which.max(sale_status == 'Succeeded')] else TRUE)

#  address                    sale_status sale_date 
#  <fct>                      <fct>       <date>    
#1 9A Eagle Point N           Succeeded   2020-03-01
#2 9999 Mineral Wells Highway Failed      2020-03-02
#3 9999 Mineral Wells Highway Failed      2019-08-01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.