使用 R 根据日期计算每年的周数

发布于 2024-12-21 12:07:12 字数 499 浏览 2 评论 0 原文

我有一个包含 2 个不同年份(2009 年和 2010 年)日期的数据集,并且希望每个日期都有相应的周数。

我的数据集与此类似:

  anim <- c(012,023,045,098,067)
  dob <- c("01-09-2009","12-09-2009","22-09-2009","10-10-2010","28-10-2010")
  mydf <- data.frame(anim,dob)
  mydf
    anim    dob
1   12   01-09-2009
2   23   12-09-2009
3   45   22-09-2009
4   98   10-10-2010
5   67   28-10-2010

我希望在第三列中有变量“周”,并为每个日期提供相应的周数。

编辑: 注意:每年第一周从 1 月 1 日开始,第二周从 1 月 8 日开始

。如有任何帮助,我们将不胜感激。

巴兹

I have a dataset with dates of 2 different years (2009 and 2010) and would like to have the corresponding week number for each date.

My dataset is similar to this:

  anim <- c(012,023,045,098,067)
  dob <- c("01-09-2009","12-09-2009","22-09-2009","10-10-2010","28-10-2010")
  mydf <- data.frame(anim,dob)
  mydf
    anim    dob
1   12   01-09-2009
2   23   12-09-2009
3   45   22-09-2009
4   98   10-10-2010
5   67   28-10-2010

I would like to have variable "week" in the third column with the corresponding week numbers for each date.

EDIT:
Note: Week one begins on January 1st, week two begins on January 8th for each year

Any help would be highly appreciated.

Baz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-12-28 12:07:12

您对“一年中的一周”的定义

编辑:注意:每年第一周从 1 月 1 日开始,第二周从 1 月 8 日开始

strftime 支持的标准不同:

%U
    Week of the year as decimal number (00–53) using Sunday as the first day 1 
    of the week (and typically with the first Sunday of the year as day 1 of 
    week 1). The US convention.
%W
    Week of the year as decimal number (00–53) using Monday as the first day 
    of week (and typically with the first Monday of the year as day 1 of week 
    1). The UK convention.

因此您需要根据一年中的日期来计算它数字。

mydf$week <- (as.numeric(strftime(as.POSIXct(mydf$dob, 
                                             format="%d-%m-%Y"), 
                                  format="%j")) %/% 7) + 1

Your definition of "week of year"

EDIT: Note: Week one begins on January 1st, week two begins on January 8th for each year

differs from the standard ones supported by strftime:

%U
    Week of the year as decimal number (00–53) using Sunday as the first day 1 
    of the week (and typically with the first Sunday of the year as day 1 of 
    week 1). The US convention.
%W
    Week of the year as decimal number (00–53) using Monday as the first day 
    of week (and typically with the first Monday of the year as day 1 of week 
    1). The UK convention.

So you need to compute it based on the day-of-year number.

mydf$week <- (as.numeric(strftime(as.POSIXct(mydf$dob, 
                                             format="%d-%m-%Y"), 
                                  format="%j")) %/% 7) + 1
×纯※雪 2024-12-28 12:07:12

Post 2011 Answer

library(lubridate)
mydf$week <- week(mydf$week)

lubridate 包对于此类日常任务非常简单。

Post 2011 Answer

library(lubridate)
mydf$week <- week(mydf$week)

lubridate package is straight-forward for day-to-day tasks like this.

時窥 2024-12-28 12:07:12

如果您想知道您感兴趣的日期和一年的第一天之间已经过去了多少周(或 7 天),无论一年的第一天是星期几,以下是解决方案(使用 lubridate 中的 floor_date)。

mydf$weeks <- difftime(mydf$dob, floor_date(mydf$dob, "year"), units = c("weeks")))

If you want to do how many weeks (or 7 day periods) have passed between your date of interest and the first day of the year, regardless of what day of the week it was on the first of the year, the following is a solution (using floor_date from lubridate).

mydf$weeks <- difftime(mydf$dob, floor_date(mydf$dob, "year"), units = c("weeks")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文