当前位置:天才代写 > tutorial > R语言教程 > R语言教程之时间序列问题汇总实现代码教学

R语言教程之时间序列问题汇总实现代码教学

2018-05-24 08:00 星期四 所属: R语言教程 浏览:699

把表明时间序列的字符串转成时刻类型数据,在R言语里面有两个根本的函数:as.POSIXlt() 和 as.POSIXct()。两者都是S3泛型函数,依据参数的数据类型挑选不同的变换办法,除字符串外还能够变换数字、因子等数据类型,适应性很强:

as.POSIXlt("2010/01/01")
## [1] "2010-01-01 CST"
as.POSIXlt("2010/01/01 10:30:30")
## [1] "2010-01-01 10:30:30 CST"
as.POSIXlt("10:30:30 2010/01/01", format = "%H:%M:%S %Y/%m/%d")
## [1] "2010-01-01 10:30:30 CST"
tt <- Sys.time() as.POSIXlt(3600 * 24 * 1001, origin = tt)
## [1] "2019-07-20 07:15:10 CST"
as.POSIXlt(3600 * 24 * 1001, origin = tt, tz = "UTC")
## [1] "2019-07-19 23:15:10 UTC"
as.POSIXct("2010/01/01")
## [1] "2010-01-01 CST"
as.POSIXct("2010/01/01 10:30:30")
## [1] "2010-01-01 10:30:30 CST"
as.POSIXct("10:30:30 2010/01/01", format = "%H:%M:%S %Y/%m/%d")
## [1] "2010-01-01 10:30:30 CST"
as.POSIXct(3600 * 24 * 1001, origin = tt)
## [1] "2019-07-20 07:15:10 CST"
as.POSIXct(3600 * 24 * 1001, origin = tt, tz = "UTC")
## [1] "2019-07-19 23:15:10 UTC"

非标准次序日期时刻字符串需求运用format参数。

那么,什么是POSIXct和POSIXlt?它们间有什么不同?咱们需求了解它们的界说:POSIXct用某个时刻到UNIX元年(1970-01-01 00:00:00)所经过的秒数来记载时刻,即经过计数的方法表明时刻(count time);而POSIXlt用列表方法表明时刻(list time),时刻的每一部分都是列表的一个元素:

(ct <- as.POSIXct("1980-09-30 10:11:12"))
## [1] "1980-09-30 10:11:12 CST"
(lt <- as.POSIXlt("1980-09-30 10:11:12"))
## [1] "1980-09-30 10:11:12 CST"
unlist(ct)
## [1] "1980-09-30 10:11:12 CST"
unlist(lt)
##    sec    min   hour   mday    mon   year   wday   yday  isdst   zone 
##   "12"   "11"   "10"   "30"    "8"   "80"    "2"  "273"    "0"  "CST" 
## gmtoff 
##     NA

lt时刻的年份是从1900年开端计数的。在没有任何辅助工具函数的条件下,从一个lt时刻中很容易取得年月日时分秒和星期等数据,而ct时刻不行:

c(1900 + lt$year, lt$mon, lt$mday, lt$hour, lt$min, lt$sec)
## [1] 1980    8   30   10   11   12

但另一方面,ct时刻的存储只需求用一个双精度数,lt则需求一个长列表,相比之下当然是ct节约资源。

尽管终究可能表明相同的时刻,但ct时刻和lt时刻类型数据不是随意能够兼并的:

c(ct, lt)
##                                                 sec 
## "1980-09-30 10:11:12 CST" "1970-01-01 08:00:12 CST" 
##                       min                      hour 
## "1970-01-01 08:00:11 CST" "1970-01-01 08:00:10 CST" 
##                      mday                       mon 
## "1970-01-01 08:00:30 CST" "1970-01-01 08:00:08 CST" 
##                      year                      wday 
## "1970-01-01 08:01:20 CST" "1970-01-01 08:00:02 CST" 
##                      yday                     isdst 
## "1970-01-01 08:04:33 CST" "1970-01-01 08:00:00 CST" 
##                      zone                    gmtoff 
##                        NA                        NA
c(lt, ct)
## [1] "1980-09-30 10:11:12 CST" "1980-09-30 10:11:12 CST"
c(NULL, ct)
## [1] 339127872
c(NULL, lt)
## $sec
## [1] 12
## 
## $min
## [1] 11
## 
## $hour
## [1] 10
## 
## $mday
## [1] 30
## 
## $mon
## [1] 8
## 
## $year
## [1] 80
## 
## $wday
## [1] 2
## 
## $yday
## [1] 273
## 
## $isdst
## [1] 0
## 
## $zone
## [1] "CST"
## 
## $gmtoff
## [1] NA
c(ct, NULL)
## [1] "1980-09-30 10:11:12 CST"
c(lt, NULL)
## Error in as.POSIXct.default(X[[i]], ...): do not know how to convert 'X[[i]]' to class "POSIXct"


2 时刻的算术运算

ct和lt能够混合运算,但只能求差值,得到的是天数。时刻和数字的加减运算是把数字当成秒对待,在次序上也有考究,详细看例子:

tt1 <- as.POSIXct("2010-11-12 10:20:30") tt2 <- as.POSIXlt("2010-11-21 10:30:40") tt1 - tt2
## Time difference of -9.00706 days
tt1 + tt2
## Error in `+.POSIXt`(tt1, tt2): binary '+' is not defined for "POSIXt" objects
tt1 - 10
## [1] "2010-11-12 10:20:20 CST"
tt1 + 10
## [1] "2010-11-12 10:20:40 CST"
tt1 - 10
## [1] "2010-11-12 10:20:20 CST"
10 + tt1
## [1] "2010-11-12 10:20:40 CST"
10 - tt1
## Error in `-.POSIXt`(10, tt1): can only subtract from "POSIXt" objects

4 字符串变换为时刻:

除了as.POSIXlt() 和 as.POSIXct()函数外,还有其他一些函数。如果载入了R言语的其他包,会有更多用法。

4.1 as.Date

用法很简单:

as.Date("2010-10-30")
## [1] "2010-10-30"
as.Date("2010/10/30")
## [1] "2010-10-30"
as.Date("20101030")
## Error in charToDate(x): character string is not in a standard unambiguous format
as.Date("20101030", format = "%Y%m%d")
## [1] "2010-10-30"

format参数用于识别输入字符串的摆放次序。

4.2 strptime与strftime

这两者称号和参数都很像,但这儿面有乌龙。先看参数:

args(strptime)
## function (x, format, tz = "") 
## NULL
args(strftime)
## function (x, format = "", tz = "", usetz = FALSE, ...) 
## NULL
strptime("2010-10-10")
## Error in strptime("2010-10-10"): 短少参数"format",也没有缺省值
strptime("2010-10-10", format = "%Y-%m-%d")
## [1] "2010-10-10 CST"
strftime("2010-10-10")
## [1] "2010-10-10"
strftime("2010-10-10", usetz = TRUE)
## [1] "2010-10-10 CST"
  • format参数在strptime中为必选,在strftime中为可选

  • strptime强制包括时区,而strftime默许不设置时区。如果strftime设置usetz=TRUE,输出成果就和strptime相同

但是,它们返回值的数据类型是彻底不相同的:

str(strptime("2010-10-10", format = "%Y-%m-%d"))
##  POSIXlt[1:1], format: "2010-10-10"
str(strftime("2010-10-10", usetz = TRUE))
##  chr "2010-10-10 CST"

strptime得到的是时刻类型数据,strftime得到字符串。检查它们的函数源代码会了解得更清楚:

strptime
## function (x, format, tz = "") 
## {
##     y <- .Internal(strptime(as.character(x), format, tz))
##     names(y$year) <- names(x)
##     y
## }
####
strftime
## function (x, format = "", tz = "", usetz = FALSE, ...) 
## format(as.POSIXlt(x, tz = tz), format = format, usetz = usetz, 
##     ...)
####

strptime函数运用的是内部C言语函数变换字符串为时刻;而strftime本质上是字符串格式化函数,先用as.POSIXlt将字符串转为时刻,然后用format函数输出字符串。

再说format参数吧,strftime尽管终究运用了strptime,但format参数并没有传递下去,而是作为format函数的参数,用于格式化输出字符串:

strptime("30-10-2010", format = "%d-%m-%Y")
## [1] "2010-10-30 CST"
strftime("30-10-2010", format = "%d-%m-%Y", tz = "CST", usetz = TRUE)
## [1] "20-10-30 CST"
strptime("2010-10-10", format = "%A")
## [1] NA
strftime("2010-10-10", format = "%A")
## [1] "周日"

strptime得到的是lt时刻。再检查as.POSIXlt对字符串处理的源代码会发现,它其实就是对strptime函数的封装。

<div id="outline-container-sec-5" class="outline-2" font-size:14px;background-color:#ffffff;"="" style="word-wrap: break-word; font-family: "sans serif", tahoma, verdana, helvetica; font-size: 12px; white-space: normal; padding: 0px; margin: 0px; color: rgb(51, 51, 51);">

5 格式化日期/时刻的字符串

经过格式化时刻,能够得到表明时刻的不同部分,也能够把时刻依照需求的方法展现出来。在处理日期时刻类型数据上,as.character、strftime和format的用法和成果是相同的:

as.character(Sys.Date(), format = "%A")
## [1] "星期六"
strftime(Sys.Date(), format = "%A")
## [1] "星期六"
format(Sys.Date(), format = "%A")
## [1] "星期六"
format(Sys.Date(), format = "%Y年%m月%d日 %A")
## [1] "2016年10月22日 星期六"

strftime灵敏一些,能直接处理代表日期时刻的字符串:

strftime("2010-10-10", format = "%A")
## [1] "周日"
as.character(as.POSIXlt("2010-10-10"), format = "%A")
## [1] "周日"
format(as.POSIXlt("2010-10-10"), format = "%A")
## [1] "周日"

不同的格式化字符代码不同的含义,详细可用“?format.POSIXct”检查:

strftime("2010-10-30 01:02:03", format = "%Y")
## [1] "2010"
strftime("2010-10-30 01:02:03", format = "%m")
## [1] "10"
strftime("2010-10-30 01:02:03", format = "%d")
## [1] "30"
strftime("2010-10-30 01:02:03", format = "%H")
## [1] "01"
strftime("2010-10-30 01:02:03", format = "%M")
## [1] "02"
strftime("2010-10-30 01:02:03", format = "%S")
## [1] "03"
strftime("2010-10-30 01:02:03", format = "%A")
## [1] "星期六"
strftime("2010-10-30 01:02:03", format = "%B")
## [1] "十月"
strftime("2010-10-30 01:02:03", format = "%c")
## [1] "2010年10月30日 星期六 01时02分03秒"

字符串的输出与locale有关,可经过LC_TIME设置以取得想要的格式。

xx <- Sys.getlocale("LC_TIME") Sys.setlocale("LC_TIME", "C")
## [1] "C"
strftime("2010-10-30 01:02:03", format = "%A")
## [1] "Saturday"
strftime("2010-10-30 01:02:03", format = "%B")
## [1] "October"
strftime("2010-10-30 01:02:03", format = "%c")
## [1] "Sat Oct 30 01:02:03 2010"
Sys.setlocale("LC_TIME", xx)
## [1] "zh_CN.utf8"

 

    关键字:

天才代写-代写联系方式