将ts对象转换为data.frame [英] Converting ts object to data.frame
本文介绍了将ts对象转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我的 ts
对象转换为 data.frame
对象。我的MWE如下:
I want to transform my ts
object to data.frame
object. My MWE is given below:
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
library(reshape2)
df <- data.frame(date=as.Date(index(dat)), Y = melt(dat)$value)
输出
Output
date Y
1 1975-05-14 86.04519
2 1975-05-14 93.78866
3 1975-05-14 88.04912
4 1975-05-15 94.30623
5 1975-05-15 72.82405
6 1975-05-15 58.31859
7 1975-05-15 66.25477
8 1975-05-16 75.46122
9 1975-05-16 86.38526
10 1975-05-16 99.48685
我在日期列中丢失了我的宿舍。如果您帮忙解决问题,我将非常感激。感谢提前。
I have lost my quarters in date columns. I would highly appreciate if you help to figure out the problem. Thanks in advance.
推荐答案
如何
data.frame(Y=as.matrix(dat), date=time(dat))
这将返回
Y date
1 86.04519 1959.25
2 93.78866 1959.50
3 88.04912 1959.75
4 94.30623 1960.00
5 72.82405 1960.25
6 58.31859 1960.50
7 66.25477 1960.75
8 75.46122 1961.00
9 86.38526 1961.25
10 99.48685 1961.50
这篇关于将ts对象转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文