Get months from dates

65 views Asked by At

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?

3

There are 3 answers

0
HoelR On BEST ANSWER
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
1
Friede On

Base R

Based on @thelatemail's note that you want unique months as result, do sort(factor(unique(unlist(lapply(df, format, "%B"))), month.name)). Written as pipe:

lapply(df, format, "%B") |>
  unlist() |>
  unique() |>
  factor(x = _, month.name) |>
  sort()

gives

[1] January  February March    May      October  November December
Levels: January February March April May June July August September October November December

Wrap in paste(place_here, collapse = ", "), if the result should be a of length one where months are comma-separated.


Data:

df = data.frame(start = as.Date(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")), 
                finish = as.Date(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")))
0
ThomasIsCoding On

You can try read.table (thanks for the data from @Friede)

> month.name[sort(unique(read.table(text = unlist(df), sep = "/")$V2))]
[1] "January"  "February" "March"    "May"      "October"  "November" "December"

or gsub

> month.name[as.numeric(sort(unique(gsub("^\\d+/|/\\d+$", "", unlist(df)))))]
[1] "January"  "February" "March"    "May"      "October"  "November" "December"