如何将数据帧转换为R中的时间序列

时间:2022-09-28 16:58:26

I have one csv file in which I have 2 closing prices of stock(on daily basis)

我有一个csv文件,其中我有2个股票收盘价(每日)

Dates   Bajaj_close Hero_close
3/14/2013   1854.8  1669.1
3/15/2013   1850.3  1684.45
3/18/2013   1812.1  1690.5
3/19/2013   1835.9  1645.6
3/20/2013   1840    1651.15
3/21/2013   1755.3  1623.3
3/22/2013   1820.65 1659.6
3/25/2013   1802.5  1617.7
3/26/2013   1801.25 1571.85
3/28/2013   1799.55 1542

I want to convert above data into time series format. (start date is 3/14/2013 and end date is 3/13/2015) I have tried this but its giving me some weird output

我想将上述数据转换为时间序列格式。 (开始日期是2013年3月14日,结束日期是2015年3月13日)我试过这个,但它给了我一些奇怪的输出

values <- bajaj_hero[, -1]  (excluded first column i.e date in real dataset)
bajaj_hero_timeseries <- ts(values,start=c(2013,1),end=c(2015,3),frequency=365)

Output is:

输出是:

           Bajaj_close Hero_close
2013.000     1854.80    1669.10
2013.003     1850.30    1684.45
2013.005     1812.10    1690.50
2013.008     1835.90    1645.60
2013.011     1840.00    1651.15
2013.014     1755.30    1623.30
2013.016     1820.65    1659.60
2013.019     1802.50    1617.70
2013.022     1801.25    1571.85

please help.. :)

请帮忙.. :)

4 个解决方案

#1


12  

R has multiple ways of represeting time series. Since you're working with daily prices of stocks, you may wish to consider that financial markets are closed on weekends and business holidays so that trading days and calendar days are not the same. However, you may need to work with your times series in terms of both trading days and calendar days. For example, daily returns are calculated from sequential daily closing prices regardless of whether a weekend intervenes. But you may also want to do calendar-based reporting such as weekly price summaries. For these reasons the xts package, an extension of zoo, is commonly used with financial data in R. An example of how it could be used with your data follows.

R有多种表示时间序列的方法。由于您正在处理每日股票价格,您可能希望考虑金融市场在周末和商务假期关闭,以便交易日和日历日不一样。但是,您可能需要根据交易日和日历日处理您的时间序列。例如,无论周末是否介入,每日回报都是从连续的每日收盘价计算得出的。但您可能还希望进行基于日历的报告,例如每周价格摘要。由于这些原因,xts包(动物园的扩展)通常与R中的财务数据一起使用。下面是一个如何与数据一起使用的示例。

Assuming the data shown in your example is in the dataframe df

假设您的示例中显示的数据位于数据帧df中

  library(xts)
  stocks <- xts(df[,-1], order.by=as.Date(df[,1], "%m/%d/%Y"))
#
#  daily returns
#
   returns <- diff(stocks, arithmetic=FALSE ) - 1
#
#  weekly open, high, low, close reports
#
   to.weekly(stocks$Hero_close, name="Hero")

which gives the output

它给出了输出

           Hero.Open Hero.High Hero.Low Hero.Close
2013-03-15    1669.1   1684.45   1669.1    1684.45
2013-03-22    1690.5   1690.50   1623.3    1659.60
2013-03-28    1617.7   1617.70   1542.0    1542.00

#2


11  

Input. We will start with the text of the input shown in the question since the question did not provide the csv input:

输入。我们将从问题中显示的输入文本开始,因为问题没有提供csv输入:

Lines <- "Dates   Bajaj_close Hero_close
3/14/2013   1854.8  1669.1
3/15/2013   1850.3  1684.45
3/18/2013   1812.1  1690.5
3/19/2013   1835.9  1645.6
3/20/2013   1840    1651.15
3/21/2013   1755.3  1623.3
3/22/2013   1820.65 1659.6
3/25/2013   1802.5  1617.7
3/26/2013   1801.25 1571.85
3/28/2013   1799.55 1542"

zoo. "ts" class series normally do not represent date indexes but we can create a zoo series that does (see zoo package):

动物园。 “ts”类系列通常不代表日期索引,但我们可以创建一个动物园系列(见动物园包):

library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")

Alternately, if you have already read this into a data frame DF then it could be converted to zoo as shown on the second line below:

或者,如果您已将其读入数据帧DF,则可将其转换为zoo,如下面的第二行所示:

DF <- read.table(text = Lines, header = TRUE)
z <- read.zoo(DF, format = "%m/%d/%Y")

In either case above z ia a zoo series with a "Date" class time index. One could also create the zoo series, zz, which uses 1, 2, 3, ... as the time index:

在上述任何一种情况下,z ia是具有“Date”类时间索引的动物园系列。还可以创建动物园系列zz,它使用1,2,3 ......作为时间索引:

zz <- z
time(zz) <- 1:nrow(zz)

ts. Either of these could be converted to a "ts" class series:

TS。这些中的任何一个都可以转换为“ts”类系列:

as.ts(z)
as.ts(zz)

The first has a time index which is the number of days since the Epoch (January 1, 1970) and will have NAs for missing days and the second will have 1, 2, 3, ... as the time index and no NAs.

第一个时间索引是自*(1970年1月1日)以来的天数,并且将具有缺失天数的NA,第二个将具有1,2,3,...作为时间索引且没有NA。

Monthly series. Typically "ts" series are used for monthly, quarterly or yearly series. Thus if we were to aggregate the input into months we could reasonably represent it as a "ts" series:

每月系列。通常,“ts”系列用于月度,季度或年度系列。因此,如果我们将输入汇总成几个月,我们可以合理地将其表示为“ts”系列:

z.m <- as.zooreg(aggregate(z, as.yearmon, mean), freq = 12)
as.ts(z.m)

#3


1  

With library fpp, you can easily create time series with date format: time_ser=ts(data,frequency=4,start=c(1954,2))

使用库fpp,您可以轻松创建日期格式的时间序列:time_ser = ts(data,frequency = 4,start = c(1954,2))

here we start at the 2nd quarter of 1954 with quarter fequency.

这里我们从1954年第二季度开始,四分之一频率。

#4


1  

See this question: Converting data.frame to xts order.by requires an appropriate time-based object, which suggests looking at argument to order.by,

看到这个问题:将data.frame转换为xts order.by需要一个合适的基于时间的对象,这建议查看order.by的参数,

Currently acceptable classes include: ‘Date’, ‘POSIXct’, ‘timeDate’, as well as ‘yearmon’ and ‘yearqtr’ where the index values remain unique.

目前可接受的类包括:'Date','POSIXct','timeDate',以及'yearmon'和'yearqtr',其中索引值保持唯一。

And further suggests an explicit conversion using order.by = as.POSIXct,

并进一步建议使用order.by = as.POSIXct进行显式转换,

df$Date <- as.POSIXct(strptime(df$Date,format),tz="UTC")
xts(df[, -1], order.by=as.POSIXct(df$Date))

Where your format is assigned elswhere,

您的格式在哪里分配,

format <- "%m/%d/%Y" #see strptime for details

#1


12  

R has multiple ways of represeting time series. Since you're working with daily prices of stocks, you may wish to consider that financial markets are closed on weekends and business holidays so that trading days and calendar days are not the same. However, you may need to work with your times series in terms of both trading days and calendar days. For example, daily returns are calculated from sequential daily closing prices regardless of whether a weekend intervenes. But you may also want to do calendar-based reporting such as weekly price summaries. For these reasons the xts package, an extension of zoo, is commonly used with financial data in R. An example of how it could be used with your data follows.

R有多种表示时间序列的方法。由于您正在处理每日股票价格,您可能希望考虑金融市场在周末和商务假期关闭,以便交易日和日历日不一样。但是,您可能需要根据交易日和日历日处理您的时间序列。例如,无论周末是否介入,每日回报都是从连续的每日收盘价计算得出的。但您可能还希望进行基于日历的报告,例如每周价格摘要。由于这些原因,xts包(动物园的扩展)通常与R中的财务数据一起使用。下面是一个如何与数据一起使用的示例。

Assuming the data shown in your example is in the dataframe df

假设您的示例中显示的数据位于数据帧df中

  library(xts)
  stocks <- xts(df[,-1], order.by=as.Date(df[,1], "%m/%d/%Y"))
#
#  daily returns
#
   returns <- diff(stocks, arithmetic=FALSE ) - 1
#
#  weekly open, high, low, close reports
#
   to.weekly(stocks$Hero_close, name="Hero")

which gives the output

它给出了输出

           Hero.Open Hero.High Hero.Low Hero.Close
2013-03-15    1669.1   1684.45   1669.1    1684.45
2013-03-22    1690.5   1690.50   1623.3    1659.60
2013-03-28    1617.7   1617.70   1542.0    1542.00

#2


11  

Input. We will start with the text of the input shown in the question since the question did not provide the csv input:

输入。我们将从问题中显示的输入文本开始,因为问题没有提供csv输入:

Lines <- "Dates   Bajaj_close Hero_close
3/14/2013   1854.8  1669.1
3/15/2013   1850.3  1684.45
3/18/2013   1812.1  1690.5
3/19/2013   1835.9  1645.6
3/20/2013   1840    1651.15
3/21/2013   1755.3  1623.3
3/22/2013   1820.65 1659.6
3/25/2013   1802.5  1617.7
3/26/2013   1801.25 1571.85
3/28/2013   1799.55 1542"

zoo. "ts" class series normally do not represent date indexes but we can create a zoo series that does (see zoo package):

动物园。 “ts”类系列通常不代表日期索引,但我们可以创建一个动物园系列(见动物园包):

library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")

Alternately, if you have already read this into a data frame DF then it could be converted to zoo as shown on the second line below:

或者,如果您已将其读入数据帧DF,则可将其转换为zoo,如下面的第二行所示:

DF <- read.table(text = Lines, header = TRUE)
z <- read.zoo(DF, format = "%m/%d/%Y")

In either case above z ia a zoo series with a "Date" class time index. One could also create the zoo series, zz, which uses 1, 2, 3, ... as the time index:

在上述任何一种情况下,z ia是具有“Date”类时间索引的动物园系列。还可以创建动物园系列zz,它使用1,2,3 ......作为时间索引:

zz <- z
time(zz) <- 1:nrow(zz)

ts. Either of these could be converted to a "ts" class series:

TS。这些中的任何一个都可以转换为“ts”类系列:

as.ts(z)
as.ts(zz)

The first has a time index which is the number of days since the Epoch (January 1, 1970) and will have NAs for missing days and the second will have 1, 2, 3, ... as the time index and no NAs.

第一个时间索引是自*(1970年1月1日)以来的天数,并且将具有缺失天数的NA,第二个将具有1,2,3,...作为时间索引且没有NA。

Monthly series. Typically "ts" series are used for monthly, quarterly or yearly series. Thus if we were to aggregate the input into months we could reasonably represent it as a "ts" series:

每月系列。通常,“ts”系列用于月度,季度或年度系列。因此,如果我们将输入汇总成几个月,我们可以合理地将其表示为“ts”系列:

z.m <- as.zooreg(aggregate(z, as.yearmon, mean), freq = 12)
as.ts(z.m)

#3


1  

With library fpp, you can easily create time series with date format: time_ser=ts(data,frequency=4,start=c(1954,2))

使用库fpp,您可以轻松创建日期格式的时间序列:time_ser = ts(data,frequency = 4,start = c(1954,2))

here we start at the 2nd quarter of 1954 with quarter fequency.

这里我们从1954年第二季度开始,四分之一频率。

#4


1  

See this question: Converting data.frame to xts order.by requires an appropriate time-based object, which suggests looking at argument to order.by,

看到这个问题:将data.frame转换为xts order.by需要一个合适的基于时间的对象,这建议查看order.by的参数,

Currently acceptable classes include: ‘Date’, ‘POSIXct’, ‘timeDate’, as well as ‘yearmon’ and ‘yearqtr’ where the index values remain unique.

目前可接受的类包括:'Date','POSIXct','timeDate',以及'yearmon'和'yearqtr',其中索引值保持唯一。

And further suggests an explicit conversion using order.by = as.POSIXct,

并进一步建议使用order.by = as.POSIXct进行显式转换,

df$Date <- as.POSIXct(strptime(df$Date,format),tz="UTC")
xts(df[, -1], order.by=as.POSIXct(df$Date))

Where your format is assigned elswhere,

您的格式在哪里分配,

format <- "%m/%d/%Y" #see strptime for details