How can I run this loop?

No worries. Is it okay if you create a separate data frame for the bacteria that can be meta-analysed? So we preserve the original dataset all_csv.

This will automate the identification and filtering of all the single-instance bacteria, and keeps all the meta-analysable studies in a new df called final_csv:


# Extract a vector of all the single bacteria studies
single_bacteria <- all_csv %>% count(Bacteria, sort = TRUE) %>% 
                   filter(n == 1) %>% select(Bacteria) %>% .$Bacteria
single_bacteria
[1] "Adlercreutzia"   "Asaccharobacter" "Atopobium"

# Keep only those bacteria that have >1 studies without having to type each bacteria out
final_csv <- all_csv[which(!all_csv$Bacteria %in% single_bacteria), ]

# Our usual code, except now all_csv is replaced with final_csv
bacteria_names <- unique(final_csv$Bacteria)

df <- data.frame(matrix(NA, ncol = 7, nrow = length(bacteria_names)))
df[, 1] <- bacteria_names
names(df) <- c("Bacteria", "estimate", "se", "zval", "pval", "ci.lb", "ci.ub")
df

for(i in 1:length(bacteria_names)){
  ma_model <- rma.mv(yi, vi, data = final_csv, subset = (Bacteria == bacteria_names[i]))
  re_table <- coef(summary(ma_model))
  df[i, -1] <- re_table
}

df

         Bacteria  estimate        se       zval         pval     ci.lb     ci.ub
1   Aeriscardovia -1.919381 0.1637011 -11.724907 9.500536e-32 -2.240229 -1.598532
2 Bifidobacterium  2.503891 0.2804214   8.929031 4.297546e-19  1.954275  3.053507
3  Brevibacterium -2.324798 0.3074499  -7.561552 3.982888e-14 -2.927389 -1.722207
1 Like