This function aggregates total feed intake across a specified date range for each location. The feed intake data is first summed per location and date, then transformed to kilograms and formatted to wide format with locations as rows and dates as columns.
all_feedintake(data, days, ref_date = Sys.Date())
A data table that must include at least 'location', 'date', 'visit_time', and 'feed_intake' columns. 'visit_time' should be convertible to date and time.
An integer specifying the number of days to include in the analysis up to and including 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 with locations as rows and formatted dates as columns. Each entry is the summed feed intake for that date and location, converted to kilograms. An additional 'all_feedintake' column is included, showing the total feed intake for each location across all dates.
This function expects that the input data is pre-cleaned and correctly formatted. Errors may occur if 'visit_time' cannot be properly converted.
# Load CSV data
data <- data.table::fread("C:/Users/Dell/Documents/projects/pptsdm_data/ppt_monitor_test_data.csv")
print(all_feedintake(data = data, days = 5))
#> Key: <location>
#> location 06-03 06-04 06-05 06-06 06-07 all_feedintake
#> <char> <num> <num> <num> <num> <num> <num>
#> 1: 501 41.30 43.52 46.97 45.56 46.34 223.69
#> 2: 502 46.95 46.13 49.73 46.41 50.10 239.32
#> 3: 503 51.22 49.40 47.49 48.06 48.47 244.65
#> 4: 504 48.68 49.71 48.49 48.85 47.98 243.70
#> 5: 505 43.64 46.99 47.73 47.40 44.47 230.23
#> 6: 506 47.60 46.10 45.98 50.43 47.72 237.83
#> 7: 507 44.87 41.04 43.98 43.24 44.58 217.71
#> 8: 508 44.22 45.83 43.71 44.74 43.99 222.50
#> 9: 509 48.30 48.09 51.44 50.91 50.07 248.80
#> 10: 510 43.86 47.92 44.62 44.72 46.01 227.12