library(tidyverse) # for data manipulation
library(lubridate) # for dates
df <- tribble( # create a sample dataframe
~Coded_Name, ~Bal_Stmt_Date, ~LEASE_EXP
, 1, 20190304, 42
, 1, 20190305, 42
, 1, 20190307, 42
, 2, 20190304, 42
, 2, 20190305, 42
, 3, 20190306, 42
, 3, 20190304, 42
)
df %>% # take the dataframe
mutate(Bal_Stmt_Date = ymd(Bal_Stmt_Date)) %>% # turn dates into dates
mutate(timeRangeOfInterest = Bal_Stmt_Date > ymd(20190303) & # create a logical variable identifying the time range of interest
Bal_Stmt_Date < ymd(20190306)) %>%
filter(timeRangeOfInterest) %>% # filter out only the time range of interest
group_by(Coded_Name) %>% # and then per company...
summarise(sum_LEASE = sum(LEASE_EXP)) # ...calculate the sum