将csv文件转换为时间序列格式

问题描述 投票:0回答:2

我正在尝试将文件导入R studio时将csv文件转换为时间序列格式。 csv数据采用以下格式:

      week1 week2 week3 week4 ...
2011    6     6     9     11
2012    11    16    18    14
2013    12    8     11    10
2014    17    16    10    7
2015    13    13    13    14
2016    9     13    16    16
2017    11    24    20    19
2018    5     14    18    13

并持续21周。

我尝试使用以下代码将数据转换为时间序列格式:

library(zoo)
con <- read.csv(file = "TS_11.csv", header = T, sep = ",")
series <- as.ts(read.zoo(con, FUN = as.yearmon))

上述代码的结果成功地将数据转换为时间序列数据,但不是我想要的格式。

series: Time-Series [1:8, 1:21] from 2011 to 2018: 6 11 12 1..

转换为时间序列时,我希望数据采用以下格式:

series: Time-Series [1:168] from 2011 to 2018: 6 11 12 1..

其中1:168包含csv文件中包含的所有数据。这与AirPassengers时间序列数据在R studio中的格式相同。我希望我的数据转换为与AirPassengers相同的时间序列格式。

r time-series
2个回答
1
投票

如果con与问题中的相同,则使用适当的起始值和频率值对其进行转置和解析。有关独立的可重现版本,请参阅注释。

ts(c(t(con)), start = start(con), frequency = ncol(con))

注意

Lines <- "year week1 week2 week3 week4
2011    6     6     9     11
2012    11    16    18    14
2013    12    8     11    10
2014    17    16    10    7
2015    13    13    13    14
2016    9     13    16    16
2017    11    24    20    19
2018    5     14    18    13"
library(zoo)
z <- read.zoo(text = Lines, header = TRUE, FUN = c)
ts(c(t(z)), start = start(z), frequency = ncol(z))

0
投票

这是一个非动物园选项:

yday <- seq(0,21)*7+1 # julian day
year <- 2011:2018 # year
g <- expand.grid(year=year, yday=yday)
g$date <- strptime(paste(g$year, g$yday, sep="-"), format = "%Y-%j", tz = "GMT")
G <- matrix(as.character(g$date), nrow = length(year), ncol = length(yday))
G <- as.data.frame(G)
L <- as.data.frame(lapply(G, as.Date))
colnames(L) <- paste0("week", seq(ncol(L)))

Result:

> L[,1:4]
       week1      week2      week3      week4
1 2011-01-01 2011-01-08 2011-01-15 2011-01-22
2 2012-01-01 2012-01-08 2012-01-15 2012-01-22
3 2013-01-01 2013-01-08 2013-01-15 2013-01-22
4 2014-01-01 2014-01-08 2014-01-15 2014-01-22
5 2015-01-01 2015-01-08 2015-01-15 2015-01-22
6 2016-01-01 2016-01-08 2016-01-15 2016-01-22
7 2017-01-01 2017-01-08 2017-01-15 2017-01-22
8 2018-01-01 2018-01-08 2018-01-15 2018-01-22
© www.soinside.com 2019 - 2024. All rights reserved.