# Make condition when one date is greater than the other in R

I would like to make a condition considering the dataset below. I believe that the if condition can be used, if not, it can be another way.

I would like to make a condition as follows:

If my chosen date2 is greater than date1 (date1 will always be just a single date, as you can see in my df dataset) the calculation will be as follows: date2 - date1 + 1 = X -> this X will be the amount of DR columns that will be deleted from my df dataset.

I will present the code of my df dataset and then an example for you to see.

df <- structure(
list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28","2021-06-28",
"2021-06-28","2021-06-28","2021-06-28"),
date2 = c("2021-04-02","2021-04-03","2021-04-08","2021-04-09","2021-04-10","2021-07-01","2021-07-02","2021-07-03"),
Week= c("Friday","Saturday","Thursday","Friday","Saturday","Thursday","Friday","Monday"),
DR01_PV = c(4,1,4,3,3,4,3,6), DR02_PV= c(4,2,6,7,3,2,7,4),DR03_PV = c(9,5,4,3,3,2,1,5),
DR04_PV = c(5,4,3,3,6,2,1,9),DR05_PV = c(5,4,5,3,6,2,1,9),
DR06_PV = c(2,4,3,3,5,6,7,8),DR07_PV = c(2,5,4,4,9,4,7,8)),
class = "data.frame", row.names = c(NA, -8L))

df
date1      date2       Week      DR01_PV DR02_PV DR03_PV DR04_PV DR05_PV DR06_PV DR07_PV
1 2021-06-28 2021-04-02   Friday       4       4       9       5       5       2       2
2 2021-06-28 2021-04-03 Saturday       1       2       5       4       4       4       5
3 2021-06-28 2021-04-08 Thursday       4       6       4       3       5       3       4
4 2021-06-28 2021-04-09   Friday       3       7       3       3       3       3       4
5 2021-06-28 2021-04-10 Saturday       3       3       3       6       6       5       9
6 2021-06-28 2021-07-01 Thursday       4       2       2       2       2       6       4
7 2021-06-28 2021-07-02   Friday       3       7       1       1       1       7       7
8 2021-06-28 2021-07-03   Monday       6       4       5       9       9       8       8

Example:

If I choose 01/07 (date2), which is a date greater than date1 (28/06), we have the following:

date2 - date1 + 1 = X

01/07 - 28/06 + 1 = 3 + 1 = 4

This 4 means that the first 4 DR columns will be deleted from my df dataset, ie DR01_PV, DR02_PV, DR03_PV and DR04_PV. My new dataset (df1) will then be:

df1
date1      date2     Week DR05_PV DR06_PV DR07_PV
1 2021-06-28 2021-04-02   Friday       5       2       2
2 2021-06-28 2021-04-03 Saturday       4       4       5
3 2021-06-28 2021-04-08 Thursday       5       3       4
4 2021-06-28 2021-04-09   Friday       3       3       4
5 2021-06-28 2021-04-10 Saturday       6       5       9
6 2021-06-28 2021-07-01 Thursday       2       6       4
7 2021-06-28 2021-07-02   Friday       1       7       7
8 2021-06-28 2021-07-03   Monday       9       8       8

If I choose 02/07 (date2), which is a date greater than date1 (28/06), we have the following:

date2 - date1 + 1 = X

02/07 - 28/06 + 1 = 4 + 1 = 5

This 5 means that the first 5 DR columns will be deleted from my df dataset, ie DR01_PV, DR02_PV, DR03_PV, DR04_PV and DR05_PV. My new dataset (df2) will then be:

df2
date1      date2     Week DR06_PV DR07_PV
1 2021-06-28 2021-04-02   Friday       2       2
2 2021-06-28 2021-04-03 Saturday       4       5
3 2021-06-28 2021-04-08 Thursday       3       4
4 2021-06-28 2021-04-09   Friday       3       4
5 2021-06-28 2021-04-10 Saturday       5       9
6 2021-06-28 2021-07-01 Thursday       6       4
7 2021-06-28 2021-07-02   Friday       7       7
8 2021-06-28 2021-07-03   Monday       8       8

I tried to insert exampl to be more understandable.

Every help is welcome!

Thank you very much!

I think I understand what you're trying to do. If you calculate the date difference and get 5, the 'dplyr' code below might be helpful. It says to start at the 4th column and remove '-' columns 4, 5, 6, 7, 8.

x <- 5
df %>% select(-(4:(x+4)))

If you have many dataframes, you can put them in a list with the corresponding date differences and loop through them with 'map'.

x <- c(5,4,3)
dfs <- list(df,df1,df2)
pruned_dfs <- map2(.x = x, .y = dfs, .f = ~select(.y, -(4:(.x+4))))

You could also use 'map' to calculate the vector of date differences 'x'. Start by pulling out dates, and then manipulate them however you like.

dfs %>% map( ~.[1,1:2])

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.