R语言-时间函数
来源:互联网 发布:蜂窝数据漫游是什么 编辑:程序博客网 时间:2024/05/16 19:21
(1)获取当前日期有两个函数 一个是Sys.Date(),另外一个是Sys.time(),获得向前向后的时间 t<-Sys.time() t [1] "2015-09-22 18:53:56 CST" t是一种长整形的数据自1970-01-01年开始,用unclass()函数得到,时间单位是秒 > unclass(t) [1] 1442919236 获取一个小时前的日期 as.POSIXlt(t-3600) [1] "2015-09-22 17:42:36 CST" 获取一天前的日期 as.POSIXlt(t-24*60*60) [1] "2015-09-21 18:42:36 CST" 同理可以可以到前20天,前20天,前一年的数据,方法比较灵活 将长整形的数据转换为日期型数据,这种方式感觉是借鉴java的,具体做法如下: t<-Sys.time() as.POSIXct(unclass(t),origin= "1970-01-01") #要从1970-01-01年开始 [1] "2015-09-22 18:53:56 CST" (2)自定义时间格式 t<-Sys.time() > t [1] "2015-09-22 19:19:54 CST" format(t,format="%Y-%m-%d") [1] "2015-09-22" format(t,format="%B-%d-%Y") [1] "九月-22-2015" format(t,format="%a-%d-%Y") [1] "周二-22-2015" format(t,format="%A-%d-%Y") [1] "星期二-22-2015" format(t,format="%b-%d-%Y") [1] "九月-22-2015" 诸如这种形式,%d代表0-31、%a代表周几,%A代表星期,%m代表月份0-12,%b代表月份,%B代表月份 (3)计算时间差 > today<-Sys.time() > today [1] "2015-09-22 19:27:03 CST" > dob<-as.Date("2014-9-22") > difftime(today,dob,units = "days") Time difference of 365.4771 days difftime(today,dob,units = "weeks") Time difference of 52.21102 weeks units可以是“auto”, “secs”, “mins”, “hours”, “days”, “weeks”其中的一个 (4)获得等差时间序列 指定起始日期: start<-as.Date("2015-01-01") > end<-as.Date("2015-09-22") > seq(from=start,to=end,by=1) [1] "2015-01-01" "2015-01-02" "2015-01-03" "2015-01-04" "2015-01-05" "2015-01-06" [7] "2015-01-07" "2015-0。。。。。。 指定长度: seq(from=start,by=1,length.out = 10) [1] "2015-01-01" "2015-01-02" "2015-01-03" "2015-01-04" "2015-01-05" "2015-01-06" [7] "2015-01-07" "2015-01-08" "2015-01-09" "2015-01-10" [plain] view plain copy now <- Sys.time() tseq <- seq(now, length.out = 100, by = "mins") 也可以用as.Date(): as.Date(0:10,origin="2015-09-10") [1] "2015-09-10" "2015-09-11" "2015-09-12" "2015-09-13" "2015-09-14" "2015-09-15" [7] "2015-09-16" "2015-09-17" "2015-09-18" "2015-09-19" "2015-09-20"
0 0
- R语言-时间函数
- R语言日期时间函数
- R语言时间处理函数
- R语言时间序列函数整理[不断更新]
- R语言时间序列函数整理
- R语言时间序列函数整理
- R语言时间序列函数整理
- R语言-时间序列函数整理
- r语言中时间函数处理
- R语言笔记-日期/时间处理函数
- R语言时间处理
- R语言时间处理
- R语言时间数据
- R语言-日期/时间
- R语言建立时间序列的两个函数
- R语言常用函数
- R语言常用函数
- R语言常用函数
- 欢迎使用CSDN-markdown编辑器
- leetcode 111 Minimum Depth of Binary Tree
- KMP算法详解
- CentOS 6.4+nginx+php+mysql详细设置步骤
- 顶层const 与底层const
- R语言-时间函数
- java-计数器
- python学习笔记注意要点
- 简单实现Redis缓存中的排序功能
- [Android] ScrollView 垂直滚动控件和 HorizontalScrollView 水平滚动控件的使用
- 21条最佳MySQL性能优化
- 浅谈设计模式之享元模式
- Contains Duplicate III
- 一位数组在String中的应用