简体   繁体   English

将数据帧转换为时间序列 R

[英]Convert data frame to time series R

I have the following data我有以下数据

data_sample
        date         Sum 
1  Feb 2015      3322.01 
2  Mar 2015      6652.77 
3  Apr 2015      3311.12 
etc

I need to convert to time series for forecasting我需要转换为时间序列进行预测

 > data <- xts(data_sample[,-1], order.by=as.Date(data_sample[,1], "%Y %m"))
Error in 1 - frac : non-numeric argument to binary operator
> data <- xts(data_sample[,-1], order.by=as.Date(data_sample[,1], "%m %Y"))
Error in 1 - frac : non-numeric argument to binary operator



> ts_ts(ts_long(data_sample))
Error in guess_time(x) : 
  No [time] column detected. To be explict, name time column as 'time'.

If you want to use as.Date(), you have to specify full dates.如果要使用 as.Date(),则必须指定完整日期。 Simply add 01 at the end of each entry.只需在每个条目的末尾添加 01。

date <- c("Feb 2015", "Mar 2015", "Apr 2015")
date <- as.Date(paste(date, "01"), format="%b %Y %d")

You can convert them back as follows,您可以按如下方式将它们转换回来,

format(date, "%b %Y")

or use as.yearmon from zoo library,或使用动物园图书馆中的 as.yearmon,

library("zoo")
as.yearmon(date)

Some examples here: Converting Date formats in R这里的一些例子: Converting Date format in R

R has multiple ways of representing time series. R 有多种表示时间序列的方式。 Since you are working with only Date and Sum, I have created a sample time series for you.由于您只使用 Date 和 Sum,因此我为您创建了一个示例时间序列。 I choose random dates and numbers.我选择随机日期和数字。

Call for Packages索取包裹

library(xts)

Create a Data Frame创建数据框

data_sample <- data.frame(
        date = as.Date(c("2012-01-01","2013-01-01","2014-01-01", )),  
        sum1 = c(3322.01, 6652.77, 3311.12))
head(data_sample)

Convert the date as in a format which R understands.将日期转换为 R 理解的格式。

rdate<- as.Date(data_sample$date, "%m/%d/%y")  
fix(rdate)

Plot the graph绘制图形

plot(data_sample$sum1~rdate,type="l",col="red")

Execution of above code will gives below output.执行上述代码将给出以下输出。

在此处输入图片说明

Assuming data_sample is as shown reproducibly in the Note at the end, convert to a time series of class zoo using read.zoo and then either use it in that form or convert it to some other class such as xts or ts using the appropriate as.* function.假设data_sample在最后的注释中可重复显示,使用read.zoo转换为类 zoo 的时间序列,然后以该形式使用它或使用适当的 as 将其转换为其他类,例如 xts 或 ts。 * 功能。 Here we used yearmon class to represent the index as that directly represents year and month without day.这里我们使用yearmon类来表示索引,因为它直接表示年和月,没有日。 This class will be used in zoo and xts and when converting to ts it will be converted appropriately.这个类将用于 zoo 和 xts,当转换为 ts 时,它将被适当地转换。

library(xts) # this also loads zoo

z <- read.zoo(data_sample, FUN = as.yearmon, format = "%b %Y")

as.xts(z)
as.ts(z)

Date日期

It is also possible to use Date class for the index in zoo and xts but that does not work well with ts class.也可以将 Date 类用于 zoo 和 xts 中的索引,但这不适用于 ts 类。 Using Date class implies that the distance between consecutive points varies according to the number of days per month as opposed to being a regularly spaced series so using Date for monthly data is normally not useful for forecasting.使用 Date 类意味着连续点之间的距离根据每月的天数而变化,而不是定期间隔的系列,因此对每月数据使用 Date 通常对预测没有用。

zd <- aggregate(z, as.Date, c)
xd <- as.xts(zd)

Note笔记

Input in reproducible form以可复制形式输入

Lines <- "date,Sum 
1,Feb 2015,3322.01 
2,Mar 2015,6652.77 
3,Apr 2015,3311.12 "
data_sample <- read.csv(text = Lines)
air1 <- type.convert(.preformat.ts(AirPassengers))
airpassengers <- as.data.frame(air1)
 
 View(airpassengers)
 class(airpassengers)
[1] "data.frame"

It converts time series data to dataframe.它将时间序列数据转换为数据帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM