Wrapper that takes any existing extract_* function, applies it to each contiguous ON-interval in the mask, and combines the interval-wise results using a length-weighted mean.

extract_with_timemask(extract_function, mask, method = "mean")

Arguments

extract_function

The function to be called.

mask

A timemask that specifies the ON intervals

method

A string that specifies how the statistics are combined between calls. Options are "mean" (default), "sum". "evaluate" and `"table"“.

Value

A function that takes in the same arguments as extract_function

Details

The returned function captures all dataframes with a time column applies the mask and then computes the statistic.

The method parameter can alternatively specify how the statistics are combined. mean would take the time weighted average (default), sum sums up each statistic, evaluate simply returns the intervals tibble of the mask with a column of statistics evaluated. table concatenates tables if the result of each call to extract_functon returned a tibble (like in extract_durations).

See also

extract_timemask() for creating time masks

Examples

player <- example_data$head_movement_data_0[[7]]
#> Error in example_data$head_movement_data_0[[7]]: subscript out of bounds
threat <- example_data$threat_movement_data_0[[7]]
#> Error in example_data$threat_movement_data_0[[7]]: subscript out of bounds

#Compute timemask where threat is moving at speed > 0.5 m/s
threat <- add_speed(threat)
#> Error: object 'threat' not found
mask <- extract_timemask(threat, speed > 0.5)
#> Error: object 'threat' not found

#Extract player speed in the same time. Warnings for erroneous intervals
extract_with_timemask(extract_speed, mask)(player)
#> Error: object 'mask' not found