Calculating the rolling average, I have tried the following:
mutate(mean10min = slide_dbl('Cleaned BPM', mean, na.rm = TRUE, .before = 3, .after = 0, .complete = TRUE))
and
mutate(rolling_avg = rollmean('Cleaned BPM', k=3, fill=NA, align='right'))
However in both cases, the new column is filled with only NAs. My data is numeric. What is the issue and how do I solve it?
The data is part of a larger dataset, imported by read_excel(). Here's the relevant extract:
I have tried rolling_avg() on simple data, and there it works, thus there must be something wrong with the format of my 'Cleaned BPM', I assume.
Cleaned BPM
requires backticks:
(See here re non-syntactic names.)
library(slider)
library(tidyverse)
# Backticks needed for non-syntactic names
tibble(`Cleaned BPM` = 1:100) |>
mutate(mean10min = slide_dbl(`Cleaned BPM`, mean, na.rm = TRUE, .before = 3, .after = 0, .complete = TRUE))
#> # A tibble: 100 × 2
#> `Cleaned BPM` mean10min
#> <int> <dbl>
#> 1 1 NA
#> 2 2 NA
#> 3 3 NA
#> 4 4 2.5
#> 5 5 3.5
#> 6 6 4.5
#> 7 7 5.5
#> 8 8 6.5
#> 9 9 7.5
#> 10 10 8.5
#> # ℹ 90 more rows
# Or clean the names first
library(janitor)
tibble(`Cleaned BPM` = 1:100) |>
clean_names() |>
mutate(mean10min = slide_dbl(cleaned_bpm, mean, na.rm = TRUE, .before = 3, .after = 0, .complete = TRUE))
#> # A tibble: 100 × 2
#> cleaned_bpm mean10min
#> <int> <dbl>
#> 1 1 NA
#> 2 2 NA
#> 3 3 NA
#> 4 4 2.5
#> 5 5 3.5
#> 6 6 4.5
#> 7 7 5.5
#> 8 8 6.5
#> 9 9 7.5
#> 10 10 8.5
#> # ℹ 90 more rows
Created on 2024-03-12 with reprex v2.1.0