monthrange python function equivalent in R

51 Views Asked by At

Hello I am trying to set up my code to accept data from an API I will be pulling. The code has already been written in python and I am trying to recreate it but in R. Here is the python code:

month = 'may'.lower()
year = '2023'
d_month_numeric = {
    'january': 1,
    'february': 2,
    'march': 3,
    'april': 4,
    'may': 5,
    'june': 6,
    'july': 7,
    'august': 8,
    'september': 9,
    'october': 10,
    'november': 11,
    'december': 12
}
month_num = d_month_numeric.get(month)
month_begin = f'{int(year)}-{month_num}-1'
month_end = f'{int(year)}-{month_num}-{monthrange(int(year), month_num)[1]}'
month_end_plusone_dt = pd.to_datetime(month_end) + dt.timedelta(days=1)
month_end_plusone = str(month_end_plusone_dt.strftime('%Y-%m-%d'))

This is what I have written out so far in R:

month= " "
    year=" "
    d_month_numeric = c('january'= 1,
    'february'=2,
    'march'= 3,
    'april'= 4,
    'may'= 5,
    'june'= 6,
    'july'= 7,
    'august'= 8,
    'september'= 9,
    'october'= 10,
    'november'= 11,
    'december'= 12)

    month_num= get("d_month_numeric")[month]
    month_begin = c(as.integer(year)- as.integer(month_num)-1)

This code seems to run without error in Rstudio but I cannot figure out what functions I need to use in R to recreate the month range part of the python code, let alone the pd.to_datetime etc etc equivalent functions in R.

This is an example output:

enter image description here

0

There are 0 best solutions below