Search code examples
rstringgroup-bycount

grouped count specific names and keeping 0-groups


I happend to have a tabble with bee observation over a few years. Each observation is listed together with the type of bee, the year and other for now irrelevant factors. I would like R to give me a tabble with the amount of observations of a specific bee AND I would like R to keep the years, where no observations of that bee where made and fill it with a zero.

Year Observations
1988 2
1989 0
1990 4

I tried:

qa <- Beedata %>% group_by(year, .drop = FALSE) %>% count(type) %>% filter(type == "ephippius")

but that didn't work.

Thank you for helping me.


Solution

  • If you want to have the year based on specific range:

    df = data.frame(
      bee = c("A","A","A","B","C"),
      year = c(2000, 2002, 2000, 2001, 1999)
    )
    
    df %>% 
      filter(bee == "A") %>% 
      group_by(year) %>% 
      summarise(obs = n()) %>% 
      tidyr::complete(
        year = 1990:2002, # Expand year from 1990 to 2002
        fill = list(obs = 0)
      )
    

    If you want the year is based on existing year in the dataset:

    df %>% 
      mutate(year = as.factor(year)) %>% 
      filter(bee == "A") %>% 
      group_by(year, .drop = FALSE) %>% 
      summarise(obs = n())