SAS里面有一个关于时间的函数,INTNX,作用是计算从开始日期经过指定个时间间隔后的日期
R里有类似的,但好像没有功能一样完善的,所以自己写了一个
写得比较麻烦,希望提出改进意见
- # 本函数作用是计算从开始日期经过指定个时间间隔后的日期。
- # DateStr是原始日期字符串变量;TimeInterval是时间间隔,=D、W、M、Q、H、Y分别表示时间间隔为日、周、月、季、半年、年,仅能设置一个;N是时间间隔的个数,负值表示向前查找;Align是标记变量,=Beginning、Sameday、End分别表示返回一个周期的开始日期、相同日期和结束日期。
- # 最后返回的是从开始日期经过指定个时间间隔后的日期。
- # Created on 2014.3.19
- # Modified on 2014.3.19
- intnx <- function(DateStr,TimeInterval,N,Align="sameday"){
- library(lubridate)
-
- stopifnot(is.character(DateStr)||is.Date(DateStr))
- stopifnot(is.numeric(N))
-
- # 检查TimeInterval的合法性
- if (length(grep("(D|W|M|Q|H|Y)",TimeInterval,ignore.case=TRUE)) == 0)
- stop("Error: The TimeInterval should be one of D, M, W, Q, H, and Y, case insensitive and with quotes.;")
-
- # 开始进行计算
- if (toupper(TimeInterval) == "D"){
- temp <- as.Date(DateStr) + N
- }
- else if (toupper(TimeInterval) == "W"){
- if (toupper(Align) == "SAMEDAY"){
- temp <- as.Date(DateStr) + N*7
- }
- else if (toupper(Align) == "BEGINNING"){
- temp <- as.Date(DateStr) + N*7
- temp <- temp-(wday(temp)-1)
- }
- else if (toupper(Align) == "END"){
- temp <- as.Date(DateStr) + N*7
- temp <- temp+(7-wday(temp))
- }
- }
- else if (toupper(TimeInterval) == "M"){
- if (toupper(Align) == "SAMEDAY"){
- temp <- as.Date(DateStr) %m+% months(N)
- }
- else if (toupper(Align) == "BEGINNING"){
- temp <- as.Date(paste(substr(as.Date(DateStr) %m+% months(N),1,8),"01",sep=""))
- }
- else if (toupper(Align) == "END"){
- temp <- as.Date(paste(substr(as.Date(DateStr) %m+% months(N+1),1,8),"01",sep=""))-1
- }
- }
- else if (toupper(TimeInterval) == "Q"){
- if (toupper(Align) == "SAMEDAY"){
- temp <- as.Date(DateStr) %m+% months(N*3)
- }
- else if (toupper(Align) == "BEGINNING"){
- temp <- as.Date(DateStr) %m+% months(N*3)
- temp <- as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/3))*3+1,"01",sep="-"))
- }
- else if (toupper(Align) == "END"){
- temp <- as.Date(DateStr) %m+% months(N*3)
- temp <- (as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/3)+1)*3,"01",sep="-")) %m+% months(1))-1
- }
- }
- else if (toupper(TimeInterval) == "H"){
- if (toupper(Align) == "SAMEDAY"){
- temp <- as.Date(DateStr) %m+% months(N*6)
- }
- else if (toupper(Align) == "BEGINNING"){
- temp <- as.Date(DateStr) %m+% months(N*6)
- temp <- as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/6))*6+1,"01",sep="-"))
- }
- else if (toupper(Align) == "END"){
- temp <- as.Date(DateStr) %m+% months(N*6)
- temp <- (as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/6)+1)*6,"01",sep="-")) %m+% months(1))-1
- }
- }
- else if (toupper(TimeInterval) == "Y"){
- if (toupper(Align) == "SAMEDAY"){
- temp <- as.Date(DateStr) %m+% years(N)
- }
- else if (toupper(Align) == "BEGINNING"){
- temp <- as.Date(DateStr) %m+% years(N)
- temp <- as.Date(paste(substr(temp,1,4),"01","01",sep="-"))
- }
- else if (toupper(Align) == "END"){
- temp <- as.Date(DateStr) %m+% years(N)
- temp <- as.Date(paste(substr(temp,1,4),"12","31",sep="-"))
- }
- }
- return(temp)
- }
- Demo <- function(){
- date <- intnx(DateStr="2014-7-4",TimeInterval="H",N=1,Align="beginning")
- }


雷达卡




京公网安备 11010802022788号







