更改 r 中时间序列的原点

Change origin for time series in r

我想使用 R 中的时间序列,从 01-01-52 到 01-01-88。 (1952 年至 1988 年)。 37 个观察结果。

然而,当我在 R 中读取它时,我遇到了一个问题,即从 01-01-52 到 01-01-68 的观测值被解释为 2052 年等,而不是 1952 年。

如何强制 R 读入从 1952 年到 1988 年的所有数据?

Link 到我的数据:https://www.dropbox.com/s/93foyc238skt3xj/AgricIndus.csv?dl=0

这是我用过的代码。您知道我需要如何处理我的代码才能使其正确阅读吗?

agri <- read.table("AgricIndus.csv",
                  sep = ",", header = TRUE, skip = 0,
                  stringsAsFactors = FALSE)


agri$time <- as.Date(agri$time, "%m-%d-%y")

agri.xts <- xts(agri[, 2:3], order.by = agri$time)

一种方式(hack)可以是:

agri$time <- as.Date(paste0(substring(agri$time,1,6), '19', substring(agri$time,7,8)), 
                                                                          "%m-%d-%Y")
agri$time
# [1] "01-01-52" "01-01-53" "01-01-54" "01-01-55" "01-01-56" "01-01-57" "01-01-58" "01-01-59" "01-01-60" "01-01-61" "01-01-62" "01-01-63" "01-01-64" "01-01-65"
# [15] "01-01-66" "01-01-67" "01-01-68" "01-01-69" "01-01-70" "01-01-71" "01-01-72" "01-01-73" "01-01-74" "01-01-75" "01-01-76" "01-01-77" "01-01-78" "01-01-79"
# [29] "01-01-80" "01-01-81" "01-01-82" "01-01-83" "01-01-84" "01-01-85" "01-01-86" "01-01-87" "01-01-88"

如果您可以确定您的时间序列是规律的,那么生成规律的日期序列可能是最简单的方法,如下所示:

agri$time <- seq.Date(as.Date("1952-01-01"),as.Date("1988-01-01"),by='years’)

另一个适用于不规则时间序列的简单解决方案是使用 format = %m-%d-%Y(大写的“Y”!)读取 52 至 88 年的数据并添加 1900 年:

df$time <- as.POSIXlt(as.Date(df$time,format = '%m-%d-%Y'))
df$time$year <-df$time$year + 1900
df$time <- as.Date(df$time)
df$time
 [1] "1952-01-01" "1953-01-01" "1954-01-01" "1955-01-01"
 [5] "1956-01-01" "1957-01-01" "1958-01-01" "1959-01-01"
 [9] "1960-01-01" "1961-01-01" "1962-01-01" "1963-01-01"
[13] "1964-01-01" "1965-01-01" "1966-01-01" "1967-01-01"
[17] "1968-01-01" "1969-01-01" "1970-01-01" "1971-01-01"
[21] "1972-01-01" "1973-01-01" "1974-01-01" "1975-01-01"
[25] "1976-01-01" "1977-01-01" "1978-01-01" "1979-01-01"
[29] "1980-01-01" "1981-01-01" "1982-01-01" "1983-01-01"
[33] "1984-01-01" "1985-01-01" "1986-01-01" "1987-01-01"
[37] "1988-01-01"