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

将数据帧转换为时间序列R,r,time-series,R,Time Series,我有以下数据 data_sample date Sum 1 Feb 2015 3322.01 2 Mar 2015 6652.77 3 Apr 2015 3311.12 etc 我需要转换为时间序列进行预测 > data <- xts(data_sample[,-1], order.by=as.Date(data_sample[,1], "%Y %m")) Error in 1 - frac : non-

我有以下数据

data_sample
        date         Sum 
1  Feb 2015      3322.01 
2  Mar 2015      6652.77 
3  Apr 2015      3311.12 
etc
我需要转换为时间序列进行预测

 > 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'.
>数据数据(长(数据样本))
猜测时间错误(x):
未检测到[time]列。为了便于解释,将时间列命名为“时间”。

R有多种表示时间序列的方法。因为您只使用日期和总和,所以我为您创建了一个时间序列示例。我随机选择日期和数字

索取包裹 创建数据帧 将日期转换为R理解的格式。 执行上述代码将给出以下输出。
R有多种表示时间序列的方法。因为您只使用日期和总和,所以我为您创建了一个时间序列示例。我随机选择日期和数字

索取包裹 创建数据帧 将日期转换为R理解的格式。 执行上述代码将给出以下输出。 如果要使用as.Date(),必须指定完整日期。 只需在每个条目的末尾添加01

date <- c("Feb 2015", "Mar 2015", "Apr 2015")
date <- as.Date(paste(date, "01"), format="%b %Y %d")
或者使用动物园图书馆的.yearmon

library("zoo")
as.yearmon(date)
这里有一些示例:

如果要使用as.Date(),则必须指定完整日期。 只需在每个条目的末尾添加01

date <- c("Feb 2015", "Mar 2015", "Apr 2015")
date <- as.Date(paste(date, "01"), format="%b %Y %d")
或者使用动物园图书馆的.yearmon

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

此处的一些示例:

假设
数据样本
如最后的注释所示,使用
read.zoo
转换为类zoo的时间序列,然后以该形式使用,或者使用适当的as.*函数将其转换为其他类,如xts或ts。这里我们使用
yearmon
类来表示索引,因为它直接表示没有日期的年份和月份。该类将在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)
library(xts)#这也会加载zoo

z假设
数据样本
如末尾注释中所示,使用
read.zoo
转换为类zoo的时间序列,然后以该形式使用它,或者使用适当的as.*函数将其转换为其他类,如xts或ts。这里我们使用
yearmon
类来表示索引,因为它直接表示没有日期的年份和月份。该类将在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)
library(xts)#这也会加载zoo
z
air1
air1
format(date, "%b %Y")
library("zoo")
as.yearmon(date)
library(xts) # this also loads zoo

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

as.xts(z)
as.ts(z)
zd <- aggregate(z, as.Date, c)
xd <- as.xts(zd)
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"