简体   繁体   English

R:为日期添加月份的有效方法?

[英]R: efficient ways to add months to dates?

I have a data.table of millions of rows and one of the columns is date column. 我有一个数百万行的data.table,其中一列是日期列。 I would like to add 12 months to all the dates in that column and create a new column. 我想在该列的所有日期添加12个月并创建一个新列。 So I use the dplyr and lubridate packages Eg 所以我使用dplyr和lubridate包Eg

library(dplyr)
library(lubridate)
new_data <- data %>% mutate(date12m = date %m+% months(12))

This works, however it is very slow for large datasets. 这可行,但对于大型数据集来说速度非常慢。 Am I missing something? 我错过了什么吗? How can this be sped up? 怎么加速呢? I generally don't expect R to run for more than 10 minutes for such a simple task 对于这么简单的任务,我通常不希望R运行超过10分钟

Edit: 编辑:

I note that my solution is already more efficient than using as.yearmon. 我注意到我的解决方案已经比使用as.yearmon更有效了。 Thanks to Colonel Beauvel for the solution 感谢Beauvel上校的解决方案

a <- data.frame(date = rep(today(),1000000))


func = function(u) {
  d = as.Date(as.yearmon(u)+1, frac=1) 
  if(day(u)>day(d)) return(d)
  day(d) = day(u)
  d
} 

pt <- proc.time()
a <- a %>% mutate(date12m = func(date))
data.table::timetaken(pt)


pt <- proc.time()
a <- a %>% mutate(date12m = date %m+% 12)
data.table::timetaken(pt)

Just add 1 with month : 只需在month添加1:

x=seq.Date(from=as.Date("2007-01-01"), to=as.Date("2014-12-12"), by="day")
month(x) = month(x) + 1

#> head(x)
#[1] "2007-02-01" "2007-02-02" "2007-02-03" "2007-02-04" "2007-02-05" "2007-02-06"

Edit : as per @akrun comment here is the solution, using as.yearmon from zoo package. 编辑 :根据@akrun评论这里是解决方案,使用来自zoo包的as.yearmon The trick is to do quick check when taking the day of the last date of the next month: 诀窍是在下个月的最后一天采取快速检查:

library(zoo)

func = function(u)
{
    d = as.Date(as.yearmon(u)+1/12, frac=1) 
    if(day(u)>day(d)) return(d)
    day(d) = day(u)
    d
} 

x=as.Date(c("2014-01-31","2015-02-28","2013-03-02"))
#> as.Date(sapply(x, func))
#[1] "2014-02-28" "2015-03-28" "2013-04-02"

I am also working with big data frames in R, you can use the package DescTools , it has a function named AddMonths(date,NoOfMonths) . 我也在使用R中的大数据框,你可以使用包DescTools ,它有一个名为AddMonths(date,NoOfMonths)的函数。

It is works quite well for me. 它对我来说效果很好。

> a <- ymd("2011-09-9")
> b <- AddMonths(a,1)
> b

[1] "2011-10-09"

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

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