-2

I have column - Month, and there are few months, I need to find out the number of day in that month.

I need to find the number of days in a month considering the leap year and that the month can go to June 2019, or current month. The logic should be applied on the column.

MONTH           no_of_days
Jan,2017
Feb,2017
Mar,2017
Apr,2017
May,2017
Jun,2017
Jul,2017
Aug,2017
Sep,2017
Oct,2017
Nov,2017
Dec,2017

structure(list(MONTH = structure(c(9L, 7L, 15L, 1L, 17L, 13L, 11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L, 4L, 24L, 22L, 20L, 6L), .Label = c("Apr,2017", "Apr,2018", "Aug,2017", "Aug,2018", "Dec,2017", "Dec,2018", "Feb,2017", "Feb,2018", "Jan,2017", "Jan,2018", "Jul,2017", "Jul,2018", "Jun,2017", "Jun,2018", "Mar,2017", "Mar,2018", "May,2017", "May,2018", "Nov,2017", "Nov,2018", "Oct,2017", "Oct,2018", "Sep,2017", "Sep,2018"), class = "factor")), class = "data.frame", row.names = c(NA, -24L))

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robby star
  • 281
  • 2
  • 13
  • Is the problem finding out how many days are in a month or is it the coding? – kath Jun 11 '19 at 07:12
  • need to find the number of days in a month how can we find out, considering the leap year and the month can go to June 2019 ,, or current month, logic should be applied on the column. – Robby star Jun 11 '19 at 07:14

3 Answers3

2

You need to first get the column MONTH to Date and then use any of the function shown here to get number of days in a month.

as.Date(paste0(1, df$MONTH), "%d%b,%Y")
#[1] "2017-01-01" "2017-02-01" "2017-03-01" "2017-04-01" "2017-05-01" "2017-06-01" 
#    "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01" "2017-11-01" "2017-12-01"

Hmisc::monthDays(as.Date(paste0(1, df$MONTH), "%d%b,%Y"))
#[1] 31 28 31 30 31 30 31 31 30 31 30 31

data

df <- structure(list(MONTH = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L, 
2L, 12L, 11L, 10L, 3L), .Label = c("Apr,2017", "Aug,2017", "Dec,2017", 
"Feb,2017", "Jan,2017", "Jul,2017", "Jun,2017", "Mar,2017", "May,2017", 
"Nov,2017", "Oct,2017", "Sep,2017"), class = "factor")), class = 
"data.frame", row.names = c(NA, -12L))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • betterDates <- as.Date(input$MONTH, format = "%B %d %y ") - but it helped, getting all NA's , please suggest – Robby star Jun 11 '19 at 08:23
  • @Robbystar Please check the answer. add `1` first since you don't have date component, also use the correct format as shown. Do `betterDates <- as.Date(paste0(1, input$MONTH), format = "%d%b %Y ")` – Ronak Shah Jun 11 '19 at 08:27
  • tried it still gettig all NA's – Robby star Jun 11 '19 at 08:28
  • class(input$MONTH) - is factor – Robby star Jun 11 '19 at 08:29
  • do you really have what you have shown here then? I have updated my post with the data I am using. Can you check how is it different from the data you have? If it is different please do `dput(input)` and update the post. – Ronak Shah Jun 11 '19 at 08:31
  • i am talking about the first step, as it is Jan, 2017, so i have to convert it into date right, somewhat 01/2017 kind of, so im stuck here – Robby star Jun 11 '19 at 08:39
  • that is what I am doing in first step. You have got dates like `Jan,2017` right? Do `as.Date(paste0(1, "Jan,2017"), "%d%b,%Y")` you get a date. Now extend this to all the values in your column. `as.Date(paste0(1, input$MONTH), "%d%b,%Y")` – Ronak Shah Jun 11 '19 at 08:44
  • its working now. Thanks – Robby star Jun 11 '19 at 08:48
1

The package lubridate has a function called days_in_month(). So you can first convert the MONTH column into dates and then use days_in_month:

library(lubridate)
library(zoo)

df <- structure(list(MONTH = structure(c(9L, 7L, 15L, 1L, 17L, 13L, 11L, 3L, 23L, 21L, 19L, 5L, 10L, 8L, 16L, 2L, 18L, 14L, 12L, 4L, 24L, 22L, 20L, 6L), .Label = c("Apr,2017", "Apr,2018", "Aug,2017", "Aug,2018", "Dec,2017", "Dec,2018", "Feb,2017", "Feb,2018", "Jan,2017", "Jan,2018", "Jul,2017", "Jul,2018", "Jun,2017", "Jun,2018", "Mar,2017", "Mar,2018", "May,2017", "May,2018", "Nov,2017", "Nov,2018", "Oct,2017", "Oct,2018", "Sep,2017", "Sep,2018"), class = "factor")), class = "data.frame", row.names = c(NA, -24L))

df$no_of_days <- days_in_month(as.Date(as.yearmon(df$MONTH, "%b,%Y")))
Sven
  • 1,203
  • 1
  • 5
  • 14
  • You haven't provided a working answer. – Paul Jun 11 '19 at 08:24
  • Why not? The OP asked "I need to find the number of days in a month considering the leap year and that the month can go to June 2019, or current month. The logic should be applied on the column." which I believe I answered. – Sven Jun 11 '19 at 08:39
  • what happens if you punch `df$no_of_days <- days_in_month(df$MONTH)` into R? What if the asker doesn't know how to convert to a date format? Your answer is incomplete and doesn't work. – Paul Jun 11 '19 at 08:49
  • With that logic, you also would need to add then he first needs to install lubridate for your example to be a working one? – Sven Jun 11 '19 at 08:57
  • If it helps, sure. – Paul Jun 11 '19 at 09:02
  • 1
    @akrun, many thanks, I've made the edits. – Sven Jun 11 '19 at 17:25
1

You'll need to wrangle your MONTH column into a date object, then you can use lubridate::days_in_month():

if (!require(lubridate)) install.packages('lubridate')
if (!require(dplyr)) install.packages('dplyr')

library(lubridate)
library(dplyr)

dat %>%
  mutate(MONTH = paste("01", MONTH, sep = ",") %>% dmy(),
         no_of_days = days_in_month(MONTH))

# A tibble: 12 x 2
   MONTH      no_of_days
   <date>          <int>
 1 2017-01-01         31
 2 2017-02-01         28
 3 2017-03-01         31
 4 2017-04-01         30
 5 2017-05-01         31
 6 2017-06-01         30
 7 2017-07-01         31
 8 2017-08-01         31
 9 2017-09-01         30
10 2017-10-01         31
11 2017-11-01         30
12 2017-12-01         31
Paul
  • 2,877
  • 1
  • 12
  • 28