How do I create an output based on a given set of dates?

I am new to R and have only very basic skills.

Based on the below dataset, if the "current_date" is 7 (or less) days before a date in the "date_1" column, in a new data frame I would like an output to be given with the appropriate "ID". That is to say, I would like the ID to be given as an output when the current date is 7 days (or less) before "date_1".

Once I get the basic syntax for this, I hope to adapt it for other functions (i.e. giving the "ID" a 30 days before or a day before the future date in the date_1 column). Thank you for your help!

print(as.tibble(Example))

A tibble: 5 x 4

 ID `Current Date`      Date_1              Date_2             


1 1001 2021-09-13 00:00:00 2021-09-20 00:00:00 2011-09-27 00:00:00
2 1002 NA 2021-09-21 00:00:00 2011-09-28 00:00:00
3 1003 NA 2021-09-22 00:00:00 2011-09-29 00:00:00
4 1004 NA 2021-09-23 00:00:00 2011-09-30 00:00:00
5 1005 NA 2021-09-24 00:00:00 2011-10-01 00:00:00

Thank you for your help!

Please post the output of

dput(as.tibble(Example))

Place a line with three back ticks before and after your output, like this
```
Your output
```

structure(list(ID = c(1001, 1002, 1003, 1004, 1005), `Current Date` = structure(c(1631491200, 
NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Date_1 = structure(c(1632096000, 1632182400, 1632268800, 
    1632355200, 1632441600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Date_2 = structure(c(1317081600, 1317168000, 1317254400, 
    1317340800, 1317427200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

Thank you for your help!

Is this the sort of thing you are looking for?

library(dplyr)

DF <- structure(list(ID = c(1001, 1002, 1003, 1004, 1005), `Current Date` = structure(c(1631491200, 
                                                                                  NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
               Date_1 = structure(c(1632096000, 1632182400, 1632268800, 
                                    1632355200, 1632441600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
               Date_2 = structure(c(1317081600, 1317168000, 1317254400, 
                                    1317340800, 1317427200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = c("tbl_df", 
                                                                                                                        "tbl", "data.frame"), row.names = c(NA, -5L))
DF$Diff <- difftime(DF$Date_1, DF$`Current Date`, units = "days")
DF
#> # A tibble: 5 x 5
#>      ID `Current Date`      Date_1              Date_2              Diff   
#>   <dbl> <dttm>              <dttm>              <dttm>              <drtn> 
#> 1  1001 2021-09-13 00:00:00 2021-09-20 00:00:00 2011-09-27 00:00:00  7 days
#> 2  1002 NA                  2021-09-21 00:00:00 2011-09-28 00:00:00 NA days
#> 3  1003 NA                  2021-09-22 00:00:00 2011-09-29 00:00:00 NA days
#> 4  1004 NA                  2021-09-23 00:00:00 2011-09-30 00:00:00 NA days
#> 5  1005 NA                  2021-09-24 00:00:00 2011-10-01 00:00:00 NA days
DF_filtered <- DF %>% filter(Diff <= 7)
DF_filtered
#> # A tibble: 1 x 5
#>      ID `Current Date`      Date_1              Date_2              Diff  
#>   <dbl> <dttm>              <dttm>              <dttm>              <drtn>
#> 1  1001 2021-09-13 00:00:00 2021-09-20 00:00:00 2011-09-27 00:00:00 7 days

Created on 2021-09-13 by the reprex package (v0.3.0)

This is what I am looking for! I can take this and run with it. I greatly appreciate your help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.