Mr Frog
Mr Frog

Reputation: 446

Getting first and last day of each month in R

I need to get the row of the first and last day of each month in a big data frame where I need to apply operations that cover accurately each month, using a for loop. Unfortunately, the data frame is not very homogeneous. Here a reproducible example to work upon:

dataframe <- data.frame(Date=c(seq.Date(as.Date("2020-01-01"),as.Date("2020-01-31"),by="day"),
    seq.Date(as.Date("2020-02-01"),as.Date("2020-02-28"),by="day"),seq.Date(as.Date("2020-03-02"),
    as.Date("2020-03-31"),by="day")))

Upvotes: 2

Views: 1840

Answers (1)

akrun
akrun

Reputation: 886938

We can create a grouping column by converting to yearmon and then get the first and last

library(zoo)
library(dplyr)
dataframe %>% 
   group_by(yearMon = as.yearmon(Date)) %>%
   summarise(FirstDay = first(Date), LastDay = last(Date))
# A tibble: 3 x 3
#  yearMon   First      Last      
#* <yearmon> <date>     <date>    
#1 Jan 2020  2020-01-01 2020-01-31
#2 Feb 2020  2020-02-01 2020-02-28
#3 Mar 2020  2020-03-02 2020-03-31

If it the first and last day irrespective of the data

library(lubridate)
dataframe %>% 
   group_by(yearMon = as.yearmon(Date)) %>%
   summarise(First = floor_date(first(Date), 'month'), 
             Last = ceiling_date(last(Date), 'month')-1)

Upvotes: 4

Related Questions