简体   繁体   English

通过星期数获取一年中的月份数

[英]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. 例如,值22应该是一年中的第5个月。 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. 我完全不确定下面的代码是否可以回答问题,就像@www在评论中说的那样,它随年份而变化,但是此代码的确获得了第5个月的信息。
The trick is to create a base date and then set the week with function lubridate::week . 技巧是创建一个基准日期,然后使用lubridate::week函数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. 我将使用lubridate库,该库使处理日期更加容易。

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. 并非lubridate :: ymd(“ 2017-01-01”)指定要查看的年份的第一周,在这种情况下为2017。如果您的数据来自多年,我建议编写一个会改变的函数此值基于df中的另一列。

I agree with @www and @RuiBarradas that Year is needed to convert Week into month. 我同意@www和@RuiBarradas的观点,即需要Year才能将Week转换为月。

I find base R functions more powerful than lubridate to handle dates in term of week and week day. 我发现base R函数比lubridate功能更强大,可以处理星期和星期几中的日期。

I have used %W from strptime format as it deals with UK format. 我从strptime格式使用%W ,因为它处理的是UK格式。 ( Monday as 1st day of week). Monday为一周的第一天)。 Alternatively %U can be used for US format ( Sunday as 1st day of week). 另外, %U可以用于US格式( Sunday为一周的第一天)。

The approach is simple. 该方法很简单。 Take Year and Week to find 1st day of that week number in that year. YearWeek为单位查找该年中该周的第一天。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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