Search code examples
rdataframedatelubridatetibble

Get months from dates


I have this tibble with a bunch of dates, below is a sample of the tibble.

library(tibble); library(lubridate)

c("25/10/1952", "27/05/1961", "07/11/1962", "20/12/1963", "15/01/1966", "12/02/1966", "12/05/1966", "11/11/1967", "14/01/1975", "17/10/1976", "05/11/1981", "03/11/1982", "14/11/1982", "12/10/1984", "24/12/1988", "26/10/1998", "11/11/2003", "14/12/2003") -> start

c("08/11/1952", "27/05/1961", "07/11/1962", "28/12/1963", "10/03/1966", "05/03/1966", "12/05/1966", "16/11/1967", "15/02/1975", "30/10/1976", "05/11/1981", "16/11/1982", "18/11/1982", "12/10/1984", "29/12/1988", "28/10/1998", "12/11/2003", "02/02/2004") -> finish

tibble(start = as.Date(dmy(start)), finish = as.Date(dmy(finish))) -> df

I need to get unique months contained in either column. So the output from the above would be this:

January, February, March, May, October, November, December

How can I achieve this?


Solution

  • library(tidyverse)
    
    df %>%
      pivot_longer(everything()) %>% 
      distinct(month = month(value, label = TRUE, abbr = FALSE)) %>% 
      arrange(month)
    
    # A tibble: 7 × 1
      month   
      <ord>   
    1 January 
    2 February
    3 March   
    4 May     
    5 October 
    6 November
    7 December
    

    Bonus, count numnber of occurences:

    df %>%
      pivot_longer(everything()) %>% 
      count(month = month(value, label = TRUE, abbr = FALSE), name = "count") 
    
    # A tibble: 7 × 2
      month    count
      <ord>    <int>
    1 January      2
    2 February     3
    3 March        2
    4 May          4
    5 October      7
    6 November    13
    7 December     5