将日期变量更改为连续月份

时间:2017-09-04 21:41:43

标签: r

我有一个数据框,其中包含一个名为“Date”的列,其格式为日期:

 df<- data.frame(date=c("1997-01-01", "1997-01-02", "1997-01-03", "1997-01-04", 
  "1997-01-05", "1997-01-06" ,"1997-01-07" ,"1997-01-08","1998-01-12", 
  "1998-01-13", "1998-01-14", "1998-01-15" ,"1998-01-16", "1998-01-17", 
   "1998-01-18", "1998-01-19"))

我需要创建一个列,将Date列中唯一的年/月组合更改为连续月份变量。我有20年的数据,可能会有1-240个月。

所以上面df的例子会返回:

output<- data.frame(date=c("1997-01-01", "1997-01-02", "1997-01-03", 
 "1997-01-04", "1997-01-05", "1997-01-06" ,"1997-01-07" ,"1997-01-08","1998-01-12", 
  "1998-01-13", "1998-01-14", "1998-01-15" ,"1998-01-16", "1998-01-17", 
   "1998-01-18", "1998-01-19"), continuous_month=c("1", "1", "1", "1", 
  "1", "1" ,"1" ,"1","13",  "13", "13", "13" ,"13", "13",  "13", "13"))

注意:01/1997将是第一个月,我在示例数据框中跳过了02/1997(第2个月)-12/1997(第12个月)的月份,因此01/1998将是第13个月系列。

4 个答案:

答案 0 :(得分:2)

您可以使用year的{​​{1}}和month来提取相关信息

lubridate

答案 1 :(得分:2)

1)常年课

"yearmon"课程内部年/月代表年份加上1月,2月,3月等的0,1 / 12,2 / 12等,所以:

library(zoo)

ym <- as.yearmon(df$date)
12 * (ym - ym[1]) + 1
## [1]  1  1  1  1  1  1  1  1 13 13 13 13 13 13 13 13

2)POSIXlt类

使用基础"POSIXlt"类的解决方案是:

with(as.POSIXlt(df$date), 12 * (year - year[1]) + mon - mon[1] + 1)
## [1]  1  1  1  1  1  1  1  1 13 13 13 13 13 13 13 13

答案 2 :(得分:1)

我们可以使用base R

df$continuous_month <- seq(1, 240, by = 12)[(as.integer(sub(".*-", "", df$date))%/%12) + 1]
df$continuous_month
#[1]  1  1  1  1  1  1  1  1 13 13 13 13 13 13 13 13

答案 3 :(得分:0)

另一个基础R解决方案在数据中添加了更多变体:

> # Format as date
> df$date <- as.Date(df$date) 
> 
> # Add some more variations to the data
> df$date <- df$date + sample(1:100, size = length(df$date)) 
> 
> # First the year difference and then the month difference.
> df$continuous_month <- (as.integer(format(df$date, "%Y")) - 1997L) * 12L +
+                        as.integer(format(df$date, "%m"))
> df
         date continuous_month
1  1997-01-28                1
2  1997-02-05                2
3  1997-01-31                1
4  1997-01-09                1
5  1997-01-15                1
6  1997-01-29                1
7  1997-03-23                3
8  1997-02-24                2
9  1998-02-20               14
10 1998-02-18               14
11 1998-01-17               13
12 1998-02-22               14
13 1998-02-01               14
14 1998-04-06               16
15 1998-02-12               14
16 1998-03-01               15