This function calculates the total feed intake for each 'house' (a subset of 'location') over a specified date range, and presents the data in a wide format. The function preprocesses the data to ensure proper formatting and then groups by 'house' and 'date' to summarize feed intake, which is then converted to kilograms.
house_feedintake(data, house_width, days, ref_date = Sys.Date())
A data table that must include at least 'location', 'date', and 'feed_intake' columns. It is expected that the data is already in a suitable format for processing.
An integer specifying the number of characters from the start of the 'location' field to define a 'house'.
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 where each row represents a date and each column a house. The entries are the summed feed intake for each house on each date, in kilograms. An additional column 'all_feedintake' shows the total feed intake across all houses for each date.
This function depends on another function 'process_data' to preprocess the data. Errors may occur if the input data or the 'process_data' function are not correctly configured to handle the data.
# Load CSV data
data <- data.table::fread("C:/Users/Dell/Documents/projects/pptsdm_data/ppt_monitor_test_data.csv")
print(house_feedintake(data = data, house_width = "1", days = 5))
#> Key: <date>
#> date 5 all_feedintake
#> <char> <num> <num>
#> 1: 06-03 460.51 460.51
#> 2: 06-04 464.41 464.41
#> 3: 06-05 469.79 469.79
#> 4: 06-06 470.22 470.22
#> 5: 06-07 469.56 469.56