How to find weighted rolling mean? I have a dataset where I want to calculate the moving average of the count variable by investigator:

I used the following code for the average means:
data_ <- data %>%
  dplyr::arrange(desc(investigator)) %>% 
  dplyr::group_by(investigator) %>% 
  dplyr::mutate(count_07da = zoo::rollmean(count, k = 7, fill = NA)) %>% 
  dplyr::ungroup()

However, each investigator interviewed multiple participants every day. We can calculate the mean value for every investigator each day, and then calculate the 7-day moving averages. However, the number of participants that interviewed varied in different days for each investigators, so it may not be accurate based on the daily means. The correct results should be weighted by the number of participants interviewed every day for each investigator. How should this be handled?
 A: Not sure if this is what you are after but maybe it helps. It's based on data.table rather than dplyr but it should be easy to adapt:
library(data.table)
library(zoo)
 
# Some test data
set.seed(1234)
df <- data.table(
    count= rpois(7000, 20),
    investigator= rep(1:10, each= 700),
    n_partecipants= sample(1:100, size= 7000, replace= TRUE)
    )

# 7-day rolling mean of count by investigator weighted by number of partecipants.
df[, roll_mean := rollapply(.SD, width= 7, function(x) {weighted.mean(x[,'count'], x[,'n_partecipants'])}, 
    by.column= FALSE, fill= NA, align= 'left'), by= investigator]

df
      count investigator n_partecipants roll_mean
   1:    14            1             49     19.31
   2:    21            1             35     19.63
   3:    21            1             87     18.88
   4:    16            1             56     16.75
   5:    22            1             23     17.64
   ...                                            

The last six values of roll_mean for each investigator will be NA because you run out of data to calculate the rolling mean.
