I have simple time tracking data that looks like this:
df = tribble(~Date, ~Name, ~Team, ~Status, ~Hours_Type, ~Hours, ~Standard, ~Deficit, ~Overtime, ~Leave,
"April 16 2021", "Jeff", "Coastal", "FT", "Billable", 40, 40, 0, 0, 0,
"April 23 2021", "Jeff", "Coastal", "FT", "Billable", 40, 40, 0, 0, 0,
"April 16 2021", "Jeff", "Coastal", "FT", "Leave", 0, 0, 0, 0, 0,
"April 23 2021", "Jeff", "Coastal", "FT", "Leave", 0, 0, 0, 0, 0,
"April 16 2021", "Megan", "Coastal", "FT", "Billable", 40, 40, 0, 0, 0,
"April 23 2021", "Megan", "Coastal", "FT", "Billable", 40, 40, 0, 0, 0,
"April 16 2021", "Megan", "Coastal", "FT", "Leave", 0, 0, 0, 0, 0,
"April 23 2021", "Megan", "Coastal", "FT", "Leave", 0, 0, 0, 0, 0,
"April 16 2021", "Minden", "Coastal", "FT", "Billable", 16, 16, 24, 0, 0,
"April 23 2021", "Minden", "Coastal", "FT", "Billable", 28, 28, 12, 0, 0,
"April 16 2021", "Minden", "Coastal", "FT", "Leave", 24, 0, 0, 0, 24,
"April 23 2021", "Minden", "Coastal", "FT", "Leave", 0, 0, 0, 0, 0)
# A tibble: 12 x 10
Date Name Team Status Hours_Type Hours Standard Deficit Overtime Leave
<chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 April 16 2021 Jeff Coastal FT Billable 40 40 0 0 0
2 April 23 2021 Jeff Coastal FT Billable 40 40 0 0 0
3 April 16 2021 Jeff Coastal FT Leave 0 0 0 0 0
4 April 23 2021 Jeff Coastal FT Leave 0 0 0 0 0
5 April 16 2021 Megan Coastal FT Billable 40 40 0 0 0
6 April 23 2021 Megan Coastal FT Billable 40 40 0 0 0
7 April 16 2021 Megan Coastal FT Leave 0 0 0 0 0
8 April 23 2021 Megan Coastal FT Leave 0 0 0 0 0
9 April 16 2021 Minden Coastal FT Billable 16 16 24 0 0
10 April 23 2021 Minden Coastal FT Billable 28 28 12 0 0
11 April 16 2021 Minden Coastal FT Leave 24 0 0 0 24
12 April 23 2021 Minden Coastal FT Leave 0 0 0 0 0
How do I query the Leave column and check if the Deficit column from the same Date should actually be 0 because it is not actually a deficit, as it is covered by leave from the same date?
For example, how would I have R check the Deficit, Leave, Name, and Date columns in order to modify this table and change Minden's April 16th 24 hour Deficit to 0 (row 9), given that it is covered by his 24 hours of Leave on April 16th (row 11)?
This would be the expected result, and the relevant code could be generalized across the entire dataset:
# A tibble: 12 x 10
Date Name Team Status Hours_Type Hours Standard Deficit Overtime Leave
<chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 April 16 2021 Jeff Coastal FT Billable 40 40 0 0 0
2 April 23 2021 Jeff Coastal FT Billable 40 40 0 0 0
3 April 16 2021 Jeff Coastal FT Leave 0 0 0 0 0
4 April 23 2021 Jeff Coastal FT Leave 0 0 0 0 0
5 April 16 2021 Megan Coastal FT Billable 40 40 0 0 0
6 April 23 2021 Megan Coastal FT Billable 40 40 0 0 0
7 April 16 2021 Megan Coastal FT Leave 0 0 0 0 0
8 April 23 2021 Megan Coastal FT Leave 0 0 0 0 0
9 April 16 2021 Minden Coastal FT Billable 16 16 0 0 0
10 April 23 2021 Minden Coastal FT Billable 28 28 12 0 0
11 April 16 2021 Minden Coastal FT Leave 24 0 0 0 24
12 April 23 2021 Minden Coastal FT Leave 0 0 0 0 0
Note: I have to keep the Leave column because I'm using it in a stacked bar chart that is visualizing this data - see in this instance the 24 Deficit for Minden should actually be Leave, but I don't know how to make this change automatically, only manually:



