This function processes a dataset to extract weights by specified house (part of location), calculates mean weights per house over a given number of days from a reference date, and returns the data in a wide format with dates as rows and houses as columns.

house_weight(data, house_width = "1", days, ref_date = Sys.Date())

Arguments

data

A data frame that must include the columns: "location", "date", and "weight".

house_width

A string indicating the number of characters to use from the 'location' column to create the 'house' identifier. Defaults to "1".

days

An integer specifying the number of days to include in the analysis back from the reference date.

ref_date

A Date object used as the reference date for filtering data. Defaults to the current system date.

Value

Returns a data frame in wide format where each column represents a house and each row represents a date within the specified range. Values are mean weights (in kilograms, rounded to two decimal places) for each house-date combination.

Note

If 'data' does not contain the necessary columns, the function will stop with an error message. It is crucial to ensure that the 'location' and 'date' columns are formatted correctly.

Examples

# Load CSV data
data <- data.table::fread("C:/Users/Dell/Documents/projects/pptsdm_data/ppt_monitor_test_data.csv")
head(house_weight(data = data, days = 5))
#> Key: <date>
#>      date house_5
#>    <char>   <num>
#> 1:  06-03   75.08
#> 2:  06-04   76.42
#> 3:  06-05   77.64
#> 4:  06-06   79.34
#> 5:  06-07   80.61