suppressWarnings(library("tidyverse"))
library(ISUmonarch)
sites = c("byd1", "byd2", "cra1", "uth1", "uth2", "uth3")

Bee data

Here is the bee data.frame in the simplest form.

pander::pandoc.table(bee %>%
  filter(siteID %in% sites) %>%
  group_by(`Nectar Plant Species`, round, siteID) %>%
  summarize(count = sum(count)) %>%
    head(.))

If we wanted to find the average count per species per siteID, we could do this:

pander::pandoc.table(bee %>%
  filter(siteID %in% sites) %>%
  group_by(`Nectar Plant Species`, round, siteID) %>%
  summarize(count = sum(count)) %>%
  group_by(`Nectar Plant Species`, siteID) %>%
  summarize(mean_count = mean(count)) %>%
    head(.))

While this table may look fine, it gives incorrect results. This is because the rounds where no data is recorded is missing. To correct this, we need to add back the rounds with 0 counts.

pander::pandoc.table(bee %>%
  mutate(round = as.factor(round)) %>%
  filter(siteID %in% sites) %>%
  group_by(`Nectar Plant Species`, round, siteID) %>%
  summarize(count = sum(count)) %>%
  complete(`Nectar Plant Species`, siteID, round, fill = list(count = 0)) %>%
  group_by(`Nectar Plant Species`, siteID) %>%
  summarize(mean_count = mean(count)) %>%
    head(.))

Notice now that alfalfa in uth2 corrctly displays a mean count of .333 since there was only 1 count accross the rounds. The key was using the complete function.



jarad/ISUmonarch documentation built on Aug. 10, 2022, 1:09 p.m.