Home > Enterprise >  Counting number of days in months in r
Counting number of days in months in r

Time:06-01

I have a dataframe with a column named date structured as bellow. Note that this is a small sample of my dataframe. I have different months and different years (my main date range is from 2005-01-03 to 2021-12-31). I want to count the number of days in each month and year combination i.e. 2 days in 2005-12, 3 days in 2006-01, ... . How can I get a vector of these counts?

df$date <- as.Date(c(
"2005-12-28", "2005-12-31", "2006-01-01", "2006-01-02", "2006-01-03", "2006-02-04", "2007-03-02", "2007-03-03", "2007-03-06", "2007-04-10", "2007-04-11"))

CodePudding user response:

library(dplyr)

df %>%
  # distinct(date) %>% # unnecessary if no dupe dates
  mutate(month = lubridate::floor_date(date, "month")) %>%
  count(month)

Result

       month n
1 2005-12-01 2
2 2006-01-01 3
3 2006-02-01 1
4 2007-03-01 3
5 2007-04-01 2

Data used:

df <- structure(list(date = structure(c(13145, 13148, 13149, 13150, 
13151, 13183, 13574, 13575, 13578, 13613, 13614), class = "Date")), row.names = c(NA, 
-11L), class = "data.frame")

CodePudding user response:

df %>% mutate(date = format(.$date, "%Y-%m")) %>% group_by(date) %>% count(date) -> out

out gives you summary by year and month as tibble.

CodePudding user response:

Here is another solution ,

a <- as.Date(c("2005-12-28", "2005-12-31", "2006-01-01",
  "2006-01-02", "2006-01-03", "2006-02-04",
  "2007-03-02", "2007-03-03", "2007-03-06",
  "2007-04-10", "2007-04-11"))

date <- strsplit(as.character(a) , "-")
# to extract months
months <- lapply(date , function(x) x[2])
# to extract years
years <- lapply(date , function(x) x[1])

table(unlist(months))
#> 
#> 01 02 03 04 12 
#>  3  1  3  2  2

table(unlist(years))
#> 
#> 2005 2006 2007 
#>    2    4    5

Created on 2022-06-01 by the reprex package (v2.0.1)

  • Related