Search code examples
rdatecountdistinct

Count unique values per month in R


I have a dataset with dead bird records from field observers.

Death.Date Observer Species Bird.ID
1 03/08/2021       DA      MF FC10682
2 15/08/2021       AG      MF FC10698
3 12/01/2022       DA      MF FC20957
4 09/02/2022       DA      MF FC10708

I want to produce a dataset from this with the number of unique Bird.ID / Month so I can produce a graph from that. ("unique" because some people make mistakes and enter a bird twice sometimes).

The output in this case would be:

Month Number of dead 
08/2021 2
01/2022 1
02/2022 1

The idea is to use the distinct function but by month (knowing the value is in date format dd/mm/yyyy).


Solution

    1. In case your Date column is character type first transform to date type with dmy

    2. Change format to month and year

    3. group_by and summarize

    library(dplyr)
    library(lubridate) # in case your Date is in character format
    
    df %>% 
      mutate(Death.Date = dmy(Death.Date)) %>% # you may not need this line
      mutate(Month = format(as.Date(Death.Date), "%m/%Y")) %>%
      group_by(Month) %>% 
      summarise(`Number of dead`=n())
    
     Month   `Number of dead`
      <chr>              <int>
    1 01/2022                1
    2 02/2022                1
    3 08/2021                2