1) 使用末尾注释中显示的数据(与问题略有不同)从温度创建一个动物园系列,其时间为年 + (month-5)/4 减少多个值在一个月内使用mean,然后将其全部转换为ts。
library(zoo)
toYearMon <- function(x) x[[1]] + (x[[2]] - 5)/4
z <- read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3,
FUN = toYearMon, aggregate = mean)
as.ts(z)
## Qtr1 Qtr2
## 2017 12.0 15.7
ts 会认为 4 个月是季度,但希望你能忍受。交替使用z。
2) 另一种可能性是,由于从 5 月 1 日到 8 月 31 日有 123 天,因此创建一个时间变量,即年 + 0 表示 May1st,年 + 1/123 表示 May2nd,.. ., 年 + 122/123 为 8 月 31 日。
toDate <- function(x) as.Date(paste(x[[1]], x[[2]], x[[3]], sep = "-"))
sinceMay1 <- function(x) {
d <- toDate(x)
may1 <- toDate(data.frame(x[[1]], 5, 1))
x[[1]] + as.numeric(d - may1) / 123
}
zsm <- read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3,
FUN = sinceMay1)
frequency(zsm) <- 123
现在我们可以使用zsm 或as.ts(zsm)
3) 如果它足以使用 1, 2, 3, ... 时代的另一种可能性是
ts(DF$Temperature)
4) 我们可以像这样创建一个动物园系列,其中 toDate 来自上方:
read.zoo(DF[c("Year", "Month", "Day", "Temperature")], index = 1:3,
FUN = toDate)
注意
我们将上个月更改为 6。
Lines <- "Year Month Day ID Size Sex Temperature
1 2017 5 13 033 54.0 M 13.0
2 2017 5 15 044 53.5 F 11.0
3 2017 6 16 141 55.8 M 15.7"
DF <- read.table(text = Lines)