如何计算R中数据框的日期列中的天数,周数,月数?

问题描述

使用format(),我可以提取年,月和日,如下所示:

date        day       month     year
<date>      <fctr>    <fctr>    <fctr>
    
2005-01-01  01        01        2005    
2005-01-01  01        01        2005    
2005-01-02  02        01        2005    
2005-01-02  02        01        2005    
2005-01-03  03        01        2005    
2005-01-03  03        01        2005    
...

2010-12-31  31        12        2010    
2010-12-31  31        12        2010    
2010-12-31  31        12        2010    
2010-12-31  31        12        2010    
2010-12-31  31        12        2010
2010-12-31  31        12        2010

但是,我也想计算从开始到结束的天数,周数,月数。也就是说,我要创建如下的日,周,月数字:

date        day       month     year     day_num    week_num    month_num
<date>      <fctr>    <fctr>    <fctr>   <double>   <double>    <double>
    
2005-01-01  01        01        2005     1          1           1
2005-01-01  01        01        2005     1          1           1
2005-01-02  02        01        2005     2          1           1
2005-01-02  02        01        2005     2          1           1
2005-01-03  03        01        2005     3          1           1
2005-01-03  03        01        2005     3          1           1
...

2005-02-28  28        02        2005     59         9           2
2005-03-01  01        03        2005     60         9           3
2005-03-02  02        03        2005     61         9           3
...

如何做到这一点而又不误算呢?

解决方法

您可以使用difftime来获取天数和周数,但是您需要一个月数的解决方法。这将达到目的:

library(lubridate)
library(dplyr)

df %>% 
  mutate(
    day_num = as.numeric(difftime(date,min(date),units = "days")),week_num = floor(as.numeric(difftime(date,units = "weeks"))),tmp = year(date) * 12 + month(date),month_num = tmp - min(tmp)
  ) %>% 
  select(-tmp)
,

format()与以下代码一起使用:

date = strptime('2005-02-28',format='%Y-%m-%d')

format(date,'%j') # Decimal day of the year

format(date,'%U') # Decimal week of the year (starting on Sunday)

format(date,'%W') # Decimal week of the year (starting on Monday)

format(date,'%m') # Decimal month

输出:

[1] "059"
[1] "09"
[1] "09"
[1] "02"

Source