楼主: playmore
1120 1

[程序分享] R程序分享1:intnx [推广有奖]

已卖:1645份资源

学科带头人

2%

还不是VIP/贵宾

-

TA的文库  其他...

R相关

经济学相关

金融工程

威望
1
论坛币
16356 个
通用积分
8.6697
学术水平
372 点
热心指数
394 点
信用等级
341 点
经验
15297 点
帖子
1194
精华
1
在线时间
1332 小时
注册时间
2007-1-11
最后登录
2025-12-1

初级学术勋章 初级热心勋章 中级热心勋章

楼主
playmore 发表于 2014-5-30 09:01:55 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
刚刚学习R,之前只会SAS
SAS里面有一个关于时间的函数,INTNX,作用是计算从开始日期经过指定个时间间隔后的日期
R里有类似的,但好像没有功能一样完善的,所以自己写了一个
写得比较麻烦,希望提出改进意见

  1. # 本函数作用是计算从开始日期经过指定个时间间隔后的日期。

  2. # DateStr是原始日期字符串变量;TimeInterval是时间间隔,=D、W、M、Q、H、Y分别表示时间间隔为日、周、月、季、半年、年,仅能设置一个;N是时间间隔的个数,负值表示向前查找;Align是标记变量,=Beginning、Sameday、End分别表示返回一个周期的开始日期、相同日期和结束日期。

  3. # 最后返回的是从开始日期经过指定个时间间隔后的日期。

  4. # Created on 2014.3.19
  5. # Modified on 2014.3.19

  6. intnx <- function(DateStr,TimeInterval,N,Align="sameday"){
  7.   library(lubridate)
  8.   
  9.   stopifnot(is.character(DateStr)||is.Date(DateStr))
  10.   stopifnot(is.numeric(N))
  11.   
  12.   # 检查TimeInterval的合法性
  13.   if (length(grep("(D|W|M|Q|H|Y)",TimeInterval,ignore.case=TRUE)) == 0)
  14.     stop("Error: The TimeInterval should be one of D, M, W, Q, H, and Y, case insensitive and with quotes.;")
  15.   
  16.   # 开始进行计算
  17.   if (toupper(TimeInterval) == "D"){
  18.     temp <- as.Date(DateStr) + N
  19.   }
  20.   else if (toupper(TimeInterval) == "W"){
  21.     if (toupper(Align) == "SAMEDAY"){
  22.       temp <- as.Date(DateStr) + N*7
  23.     }
  24.     else if (toupper(Align) == "BEGINNING"){
  25.       temp <- as.Date(DateStr) + N*7
  26.       temp <- temp-(wday(temp)-1)
  27.     }
  28.     else if (toupper(Align) == "END"){
  29.       temp <- as.Date(DateStr) + N*7
  30.       temp <- temp+(7-wday(temp))
  31.     }
  32.   }
  33.   else if (toupper(TimeInterval) == "M"){
  34.     if (toupper(Align) == "SAMEDAY"){
  35.       temp <- as.Date(DateStr) %m+% months(N)
  36.     }
  37.     else if (toupper(Align) == "BEGINNING"){
  38.       temp <- as.Date(paste(substr(as.Date(DateStr) %m+% months(N),1,8),"01",sep=""))
  39.     }
  40.     else if (toupper(Align) == "END"){
  41.       temp <- as.Date(paste(substr(as.Date(DateStr) %m+% months(N+1),1,8),"01",sep=""))-1
  42.     }
  43.   }
  44.   else if (toupper(TimeInterval) == "Q"){
  45.     if (toupper(Align) == "SAMEDAY"){
  46.       temp <- as.Date(DateStr) %m+% months(N*3)
  47.     }
  48.     else if (toupper(Align) == "BEGINNING"){
  49.       temp <- as.Date(DateStr) %m+% months(N*3)
  50.       temp <- as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/3))*3+1,"01",sep="-"))
  51.     }
  52.     else if (toupper(Align) == "END"){
  53.       temp <- as.Date(DateStr) %m+% months(N*3)
  54.       temp <- (as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/3)+1)*3,"01",sep="-")) %m+% months(1))-1
  55.     }
  56.   }
  57.   else if (toupper(TimeInterval) == "H"){
  58.     if (toupper(Align) == "SAMEDAY"){
  59.       temp <- as.Date(DateStr) %m+% months(N*6)
  60.     }
  61.     else if (toupper(Align) == "BEGINNING"){
  62.       temp <- as.Date(DateStr) %m+% months(N*6)
  63.       temp <- as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/6))*6+1,"01",sep="-"))
  64.     }
  65.     else if (toupper(Align) == "END"){
  66.       temp <- as.Date(DateStr) %m+% months(N*6)
  67.       temp <- (as.Date(paste(substr(temp,1,4),(floor((month(temp)-1)/6)+1)*6,"01",sep="-")) %m+% months(1))-1
  68.     }
  69.   }
  70.   else if (toupper(TimeInterval) == "Y"){
  71.     if (toupper(Align) == "SAMEDAY"){
  72.       temp <- as.Date(DateStr) %m+% years(N)
  73.     }
  74.     else if (toupper(Align) == "BEGINNING"){
  75.       temp <- as.Date(DateStr) %m+% years(N)
  76.       temp <- as.Date(paste(substr(temp,1,4),"01","01",sep="-"))
  77.     }
  78.     else if (toupper(Align) == "END"){
  79.       temp <- as.Date(DateStr) %m+% years(N)
  80.       temp <- as.Date(paste(substr(temp,1,4),"12","31",sep="-"))
  81.     }
  82.   }

  83.   return(temp)
  84. }

  85. Demo <- function(){
  86.   date <- intnx(DateStr="2014-7-4",TimeInterval="H",N=1,Align="beginning")
  87. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:int R程序 Beginning Modified Interval 程序

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
dxystata + 20 + 20 + 1 + 1 精彩帖子
jmpamao + 60 + 60 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 80  论坛币 + 80  学术水平 + 2  热心指数 + 2  信用等级 + 1   查看全部评分

playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

沙发
yywan0913 在职认证  发表于 2014-5-30 09:48:56
base包
lubridate包
scales包  
都有对应函数实现吧。。
是什么给了你自信

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 23:51