Let
Data_Frame <- data.frame(Col1 = c("A1", "A1", "A1", "A2", "A2", "A2", "A3", "A3", "A3"),
Col2 = c("2011-03-11", "2014-08-21", "2016-01-17", "2017-06-30", "2018-07-11", "2018-11-28", "2019-09-04", "2020-02-29", "2020-07-12"),
Col3 = c("2018-10-22", "2019-05-24", "2020-12-25", "2018-10-12", "2019-09-24", "2020-12-19", "2018-10-22", "2019-06-14", "2020-12-20"),
Col4 = c(4, 2, 2, 1, 4, 4, 4, 4, 4),
Col5 = c(7, 6, 3, 1, 3, 2, 5, 1, 2))
Data_Frame$Col2 <- as.Date(Data_Frame$Col2)
Data_Frame$Col3 <- as.Date(Data_Frame$Col3)
Data_Frame$Col1 <- as.factor(Data_Frame$Col1)
Data_Frame <- Data_Frame %>% group_by(Col1) %>% mutate(Col6 = lubridate::time_length(lubridate::interval(Col2, max(Col3)), "years"))
Data_Frame <- Data_Frame %>% group_by(Col1) %>% dplyr::mutate(Col7 = ifelse(Col6 <= 1, 1, ifelse(Col6 >1 & Col6 <=2, 2, ifelse(Col6 >2 & Col6 <=5, 5, ifelse(Col6 >5 & Col6 <=10, 10, 11)))))
Data_Frame <- as.data.frame(Data_Frame)
be the dataframe where Col6 represents the time difference between Col2 and Col3 with elements of Col2 subtracted from the biggest date element from Col3 in each group A1 to A3 in Col1, and Col7 represents which elements in Col6 is <=1, <=2, <=5 and <=10.
There are problems in additional columns generated with different conditions.
- Generation of Last1Col7 to Last10Col7:
New columns Last1Col7 to Last10Col7 are created based on Col7 and groups A1 to A3 in Col7 such that
- Last1Col7 represents how many elements (number of rows) in Col7 are <=1 in each group,
- Last2Col7 corresponds to number of rows <=2 in each group,
- Last5Col7 corresponds to number of rows <=5 in each group and so on.
The expected result is:

The following code:
Data_Frame1 <- Data_Frame %>% group_by(Col1) %>% dplyr::mutate(Last1Col7 = nrow(Data_Frame[Data_Frame$Col7 <= 1, ]),
Last2Col7 = nrow(Data_Frame[Data_Frame$Col7 <= 2, ]),
Last5Col7 = nrow(Data_Frame[Data_Frame$Col7 <= 5, ]),
Last10Col7 = nrow(Data_Frame[Data_Frame$Col7 <= 10, ]))
leads to a totally different result:

Generation of Last1SumCol4Col7 to Last10SumCol4Col7:
Last1SumCol4Col7 is the sum of entries in Col4 corresponding to how many entries (number of rows) in Col7 are <=1 in each group A1 to A3 in Col1,
Last2SumCol4Col7 is the sum of entries in Col4 corresponding to how many entries (number of rows) in Col7 are <=2 in each group A1 to A3 in Col1,
Last5SumCol4Col7 is the sum of entries in Col4 corresponding to how many entries (number of rows) in Col7 are <=5 in each group A1 to A3 in Col1,
Last10SumCol4Col7 is the sum of entries in Col4 corresponding to how many entries (number of rows) in Col7 are <=10 in each group A1 to A3 in Col1
Using the following code:
Data_Frame1 <- Data_Frame %>% group_by(Col1) %>% dplyr::mutate(Last1SumCol4Col7 = sum(Data_Frame[Data_Frame$Col7 <=1, ]$Col4),
Last2SumCol4Col7 = sum(Data_Frame[Data_Frame$Col7 <=2, ]$Col4),
Last5SumCol4Col7 = sum(Data_Frame[Data_Frame$Col7 <=5, ]$Col4),
Last10SumCol4Col7 = sum(Data_Frame[Data_Frame$Col7 <=10, ]$Col4))
Starting with columns where all the initial entries of Last1Col7 to Last10Col7 and Last1SumCol4Col7 to Last10SumCol4Col7 are zeros and then using the codes above doesn't help either. What is it that is fundamentally going wrong in the codes under 1 and 3?

