R: How to Get the Week Number of the Month

How to get week number from one month dates using R


df$week_in_month <- 1 + 
ceiling(as.numeric(difftime(as.Date(df$Input_Dates),
as.Date(paste0(substr(df$Input_Dates,1,8),'01')), units = "weeks")))

Output is:

  Input_Dates week_in_month
1 2012-02-01 1
2 2012-02-07 2
3 2012-02-15 3
4 2012-02-22 4
5 2012-02-28 5

Sample data:

df <- structure(list(Input_Dates = c("2012-02-01", "2012-02-07", "2012-02-15", 
"2012-02-22", "2012-02-28")), .Names = "Input_Dates", class = "data.frame", row.names = c(NA,
-5L))

Get the month from the week of the year

The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate package weeks() function will add the dates corresponding to the end of the relevant week. Note for example I've added an additional case in your 'ex' variable to the 52nd week, and it returns Dec-31st

library(lubridate)

ex <- c('2012-41','2016-4','2018-52')

dates <- strsplit(ex,"-")
dates <- sapply(dates,function(x) {
year_week <- unlist(x)
year <- year_week[1]
week <- year_week[2]
start_date <- as.Date(paste0(year,'-01-01'))
date <- start_date+weeks(week)
#note here: OP asked for beginning of week.
#There's some ambiguity here, the above is end-of-week;
#uncommment here for beginning of week, just subtracted 6 days.
#I think this might yield inconsistent results, especially year-boundaries
#hence suggestion to use end of week. See below for possible solution
#date <- start_date+weeks(week)-days(6)

return (as.character(date))
})

Yields:

> dates
[1] "2012-10-14" "2016-01-29" "2018-12-31"

And to simply get the month from these full dates:

month(dates)

Yields:

> month(dates)
[1] 10 1 12

how to determine month from year and week number?

We can paste Week day , week number and year together to convert it into Date and then use format to get the month name. Read ?strptime for formatting options

format(as.Date(paste(1, df$Week, df$Year), "%u %U %Y"), "%B")
#[1] "December" "May" "November"

data

df <- structure(list(Week = c(52L, 20L, 47L), Year = 2018:2016), 
class = "data.frame", row.names = c(NA, -3L))

R specify what week in a month

Using lubridate you can use isoweek to define the week column.

library(lubridate)
df[, wk := isoweek(date)]

Which gives you

#           date   weekday COMP week       RET wk
# 1: 1981-01-02 Friday 1 1 -0.005435 1
# 2: 1981-01-05 Monday 1 1 0.040984 2
# 3: 1981-01-06 Tuesday 1 1 -0.015748 2
# 4: 1981-01-07 Wednesday 1 1 -0.021333 2
# 5: 1981-01-08 Thursday 1 2 0.002725 2
# 6: 1981-01-09 Friday 1 2 0.010870 2
# 7: 1981-01-12 Monday 1 2 0.024194 3
# 8: 1981-01-13 Tuesday 1 2 -0.002625 3
# 9: 1981-01-14 Wednesday 1 2 0.013158 3
# 10: 1981-01-15 Thursday 1 3 0.033766 3
# 11: 1981-01-16 Friday 1 3 0.000000 3
# 12: 1981-01-19 Monday 1 3 -0.007538 4
# 13: 1981-01-20 Tuesday 1 3 -0.005063 4
# 14: 1981-01-21 Wednesday 1 3 0.000000 4
# 15: 1981-01-22 Thursday 1 4 -0.002545 4
# 16: 1981-01-23 Friday 1 4 0.015306 4
# 17: 1981-01-26 Monday 1 4 0.017588 5
# 18: 1981-01-27 Tuesday 1 4 -0.007407 5
# 19: 1981-01-28 Wednesday 1 4 0.024876 5
# 20: 1981-01-29 Thursday 1 5 -0.009709 5

Using dplyr, you can add the week column with

library(dplyr)
df %>%
mutate(wk = isoweek(date))

Start a week from given day and get week number in R

We may specify the week_start in floor_date (week_start - 1 would be Monday and 7 would be Sunday)

f1 <- function(dates, wk_start = 1) {
new <- lubridate::floor_date(dates, 'week', week_start = wk_start)
match(new, unique(new))
}

-checking

> library(dplyr)
> df %>%
+ mutate(week = f1(date, wk_start = 1)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 2
4 2022-01-04 2
5 2022-01-05 2
6 2022-01-06 2
> df %>%
+ mutate(week = f1(date, wk_start = 2)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 1
4 2022-01-04 2
5 2022-01-05 2
6 2022-01-06 2
> df %>%
+ mutate(week = f1(date, wk_start = 3)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 1
4 2022-01-04 1
5 2022-01-05 2
6 2022-01-06 2


Related Topics



Leave a reply



Submit