简体   繁体   中英

Get the number of the month in the year by the number of week

I have a column that has number of a week in the year:

> merged.tables_d[,"Promo2SinceWeek"]
         Promo2SinceWeek
      1:              NA
      2:              NA
      3:              NA
      4:              NA
      5:              NA
     ---                
1017205:              22
1017206:              22
1017207:              22
1017208:              22
1017209:              22

For exemple for the value 22 should be the 5 th month of the year. So The result should be like this:

> merged.tables_d[,"Promo2SinceWeek"]
             Promo2SinceWeek
          1:              NA
          2:              NA
          3:              NA
          4:              NA
          5:              NA
         ---                
    1017205:              5
    1017206:              5
    1017207:              5
    1017208:              5
    1017209:              5

How can I do this please?

I am not at all sure the code below answers to the question, like @www said in the comments, this varies with the year, but this code does get month 5.
The trick is to create a base date and then set the week with function lubridate::week .

library(lubridate)

x <- ymd("2018-01-01")
week(x) <- 22
as.integer(format(x, "%m"))
#[1] 5

You can make of this code a function, to generalize it to other years.

getMonthFromWeek <- function(Week, Year = 2018){
    x <- ymd(paste0(Year, "-01-01"))
    week(x) <- Week
    as.integer(format(x, "%m"))
}

getMonthFromWeek(22)
#[1] 5

I would use the lubridate library which makes handling dates easier.

library(lubridate)

Create dummy data:

Promo2SinceWeek <- c(1, 3, 22, 45, 52)
df <- data.frame(Promo2SinceWeek)

Return month:

month(lubridate::ymd("2017-01-01") + lubridate::weeks(df$Promo2SinceWeek - 1))

Not that lubridate::ymd("2017-01-01") specifies the first week of the month of the year you are looking at, in this case 2017. If your data is from multiple years I would suggest writing a function which changes this value based on another column in your df.

I agree with @www and @RuiBarradas that Year is needed to convert Week into month.

I find base R functions more powerful than lubridate to handle dates in term of week and week day.

I have used %W from strptime format as it deals with UK format. ( Monday as 1st day of week). Alternatively %U can be used for US format ( Sunday as 1st day of week).

The approach is simple. Take Year and Week to find 1st day of that week number in that year. One can find month easily once date has been found.

#Data
merged.tables_d <- data.frame(sl = 1:6,
                              Promo2SinceWeek = c(4, 14, 22, 24, 30, 35))

# Add another column as month
merged.tables_d$month = as.character(as.Date(
          paste(1,merged.tables_d$Promo2SinceWeek,"2018",sep = "/"),"%u/%W/%Y"),
           "%m")


merged.tables_d
#  sl Promo2SinceWeek month
#1  1               4    01
#2  2              14    04
#3  3              22    05
#4  4              24    06
#5  5              30    07
#6  6              35    08

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM