Learn R Programming

elpatron (version 0.0.4)

roll_mean.time: Roll over time windows.

Description

A less efficient implementation of roll_mean that can roll over inconsistently sampled data by a specified time window.

Usage

roll_mean.time(x, time, window, na.rm = TRUE)

Arguments

x
numeric vector to be rolled over.
time
numeric vector of sampling time values.
window
integer; the window size in time units.
na.rm
logical; should NAs be discarded?

Value

A vector of the same length as x.

Details

In essence, this function iterates of elements in x, looking back through previous elements until the corresponding element in time exceeds the specified window. For example, if the iteration was at x[100], where time[100] == 60 (seconds), a cumulative mean would be applied to previous values of x until time[i] == 30, if window = 30.

Examples

Run this code
library(dplyr, warn.conflicts = FALSE)
data(chaingang)

# 'chaingang' is sampled uniformly at 1 Hz, but lets
# pretend it's messy...
chaingang <- sample_n(chaingang, 1500) %>% arrange(time.s)

diff(chaingang$time.s) %>% table

# With this function, we can still get a 30 second rolling
# average:
chaingang <- mutate(chaingang,
                    power.30mean = roll_mean.time(power.W, time.s, 30))

plot(power.W ~ time.s, type = "l", col = "gray", data = chaingang)
lines(power.30mean ~ time.s, data = chaingang)

Run the code above in your browser using DataLab