使用 read.zoo 读取时间序列表
我查遍了所有地方,但找不到以前问过这个问题的地方。
将这些数据放入适当的动物园系列中的干净方法是什么?此版本是复制/粘贴,以使这篇文章更容易,但它将始终采用下表形式(来自文本文件)。我的 read.zoo() 语句将年份读取为索引,但将季度(Qtr1、Qtr2 等)读取为列名称。我一直在试图找出一种非垃圾的方法来将列读取为索引的“四分之一”部分,但它很草率(太草率而无法发布)。我猜这个问题已经解决了,但我找不到它。
> texinp <- "
+ Year Qtr1 Qtr2 Qtr3 Qtr4
+ 1992 566 443 329 341
+ 1993 344 212 133 112
+ 1994 252 252 199 207"
> z <- read.zoo(textConnection(texinp), header=TRUE)
> z
从 as.yearqtr() 文档来看,目标如下所示:
1992 Q1 1992 Q2 1992 Q3 1992 Q4 1993 Q1 1993 Q2 1993 Q3 1993 Q4
566 443 329 341 344 212 133 112
1994 Q1 1994 Q2 1994 Q3 1994 Q4
252 252 199 207
I've looked all over the place, but I can't find where this question has been asked before.
What is a clean way to get this data into a proper zoo series? This version is a copy/paste to make this post easier, but it will always come in the following table form (from a text file). My read.zoo() statement reads the Year as the index but the quarters (Qtr1, Qtr2, etc) are read as column names. I've been trying to figure out a non-garbage way to read the columns as the "quarter" part of the index, but it's sloppy (too sloppy to post). I'm guessing this problem has already been solved, but I can't find it.
> texinp <- "
+ Year Qtr1 Qtr2 Qtr3 Qtr4
+ 1992 566 443 329 341
+ 1993 344 212 133 112
+ 1994 252 252 199 207"
> z <- read.zoo(textConnection(texinp), header=TRUE)
> z
From the as.yearqtr() documentation, the target would look like:
1992 Q1 1992 Q2 1992 Q3 1992 Q4 1993 Q1 1993 Q2 1993 Q3 1993 Q4
566 443 329 341 344 212 133 112
1994 Q1 1994 Q2 1994 Q3 1994 Q4
252 252 199 207
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
read.zoo
读取数据,然后将其转换为具有yearqtr
时间索引的zooreg
对象:结果如下所示:
Read in the data using
read.zoo
and then convert it to azooreg
object withyearqtr
time index:The result looks like this:
read.zoo
假设您的数据最多有一个时间索引列,因此您必须自己处理这一列。首先使用read.table
读取它,然后使用
reshape
包中的melt
函数将其转换为“长表”:最后创建您想要的
zoo
对象:read.zoo
assumes your data has at most one time-index column, so you have to process this yourself. First read it in usingread.table
then convert it to a "long table" using the
melt
function from thereshape
package:and finally create your desired
zoo
object: