This function processes a dataset to identify and summarize the occurrences of NA values in the 'responder' column within a specified number of days from a reference date. It converts dates, removes duplicates, and aggregates NA counts per location and date. The output is presented in a wide format with locations as rows and formatted dates as columns.
responder_na(data, days, ref_date = Sys.Date())
A data table that must include the columns: 'location', 'date', 'visit_time', and 'responder'.
An integer specifying the number of days to include in the analysis back from the reference date.
A Date object used as the reference date for filtering data; defaults to the current system date.
Returns a data table in wide format where each row represents a location and each column a date. Entries are the counts of NAs found in the 'responder' column for that location and date. An additional 'total_nas' column shows the sum of NAs across all dates for each location.
This function stops execution and produces an error if the input data does not contain all the required columns. Ensure that 'location', 'date', 'visit_time', and 'responder' columns are formatted correctly and present.
# Load CSV data
data <- data.table::fread("C:/Users/Dell/Documents/projects/pptsdm_data/ppt_monitor_test_data.csv")
print(responder_na(data = data, days = 5))
#> location 06-03 06-04 06-05 06-06 06-07 total_nas
#> <char> <int> <int> <int> <int> <int> <num>
#> 1: 501 0 0 0 0 0 0
#> 2: 502 2 1 1 3 2 9
#> 3: 503 0 2 2 0 0 4
#> 4: 504 2 0 2 0 0 4
#> 5: 505 0 0 0 0 0 0
#> 6: 506 0 0 0 0 0 0
#> 7: 507 1 1 1 0 0 3
#> 8: 508 0 0 0 0 1 1
#> 9: 509 0 0 1 0 2 3
#> 10: 510 0 1 2 1 0 4