【发布时间】:2022-01-10 15:41:10
【问题描述】:
我在 R 中有一个数据框 df:
month abc1 def2 xyz3
201201 1 2 4
201202 2 5 7
201203 4 11 4
201204 6 23 40
我想使用decompose 函数将每一列(其中有约 50 个,每列有约 100 个每月观察值)转换为时间序列格式,以便检查数据中的季节性。
我认为使用ts 函数的for 循环将是执行此操作的最佳方式。我想在下面的循环中使用一些东西,尽管我意识到使用<- 左侧的函数会产生错误。有没有办法动态命名循环生成的变量?
for(i in 2:ncol(df)) {
paste(names(df[, i]), "_ts") <- ts(df[ ,i], start = c(2012, 1), end = c(2021,11), frequency = 12)
}
【问题讨论】:
标签: r for-loop time-series