I have a data frame with a trial column, an index column and a time series (Fy) data column. I would like to use filtfilt() on each trial independently, and the result will be the original data frame with the time series data column filtered.
Example dataset (df):
Trial | Index | Fy |
---|---|---|
1 | 1 | 1.3 |
1 | 2 | 1.4 |
1 | 3 | 1.5 |
1 | 4 | 1.6 |
2 | 5 | 2.4 |
2 | 6 | 2.5 |
2 | 7 | 2.6 |
2 | 8 | 2.7 |
My filter (from the signal package):
bf <- butter(4, 30/(1000/2), type="low")
I can use filtfilt() on a full column like:
df$Fy <- filtfilt(bf, df$Fy)
However, this doesn't allow the filter to recognize that their are different trials in the column, and each trial needs to be filtered seperately.
I have tried:
df %>%
group_by(Trial) %>%
filtfilt(bf, df$Fy) #filters specific data column
And then I tried creating a list of indices by trial:
index <- df %>%
group_by(Trial) %>%
summarise(Indices = paste(sort(unique(Index)), collapse = " "))
And tried lapply for the specific column I'm trying to filter:
df$Fy <- lapply(index, function(x) filtfilt(bf, x))