将数据框转换为xts对象

时间:2018-10-18 13:06:51

标签: r time-series xts zoo

我有一个类似于此虚拟数据的数据帧(时间序列):

df <- data.frame(stringsAsFactors=FALSE,
      symbol = c("N2", "NJ", "K-Kl", "K-P3", "K-N", "KP+", "K13", "KS",
                 "KTotal", "P500", "P800", "P23", "P55", "PA", "PKA"),
        date = c("2017-10-12", "2017-10-12", "2017-10-12", "2017-10-12",
                 "2017-10-12", "2017-10-12", "2017-10-12", "2017-10-12",
                 "2017-10-12", "2017-10-12", "2017-10-12", "2017-10-12", "2017-10-12",
                 "2017-10-12", "2017-10-12"),
     open_pr = c(10.2, 2.7, 0.5, 4.5, 2.9, 8.1, 2.3, 1, 43.2, 28.5, 5.8, 6.7,
                 5.7, 0.1, 10),
       gross = c(460L, 121L, 21L, 203L, 130L, 363L, 102L, 45L, 1946L, 1282L,
                 262L, 303L, 256L, 6L, 449L),
     avg_aud = c(19L, 3L, 0L, 5L, 5L, 21L, 4L, 1L, 153L, 92L, 10L, 14L, 6L, 0L,
                 27L),
          ts = c(59L, 32L, 31L, 34L, 57L, 83L, 59L, 28L, 113L, 103L, 53L, 69L,
                 33L, 4L, 87L),
          tv = c(6L, 1L, 0L, 2L, 2L, 7L, 1L, 0L, 49L, 29L, 3L, 5L, 2L, 0L, 9L)
)

head(df)

   symbol       date open_pr gross avg_aud  ts tv
1      N2 2017-10-12    10.2   460      19  59  6
2      NJ 2017-10-12     2.7   121       3  32  1
3    K-Kl 2017-10-12     0.5    21       0  31  0
4    K-P3 2017-10-12     4.5   203       5  34  2
5     K-N 2017-10-12     2.9   130       5  57  2

我的代码段

df %>% 
  as.tbl() %>% 
  mutate(date = ymd(date)) %>% 
  as.xts(date_col = date)

错误消息

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

我想将此数据帧转换为xts对象。与股市数据类似的东西

library(quamtmod)
x <- getSymbols("GOOG", auto.assign = FALSE)

结果:

           GOOG.Open GOOG.High  GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
2007-01-03  231.4944  236.7899  229.0652   232.2842    15513200      232.2842
2007-01-04  232.9847  240.4114  232.6618   240.0686    15877700      240.0686
2007-01-05  239.6910  242.1749  237.5102   242.0209    13833500      242.0209
2007-01-08  242.2693  243.3522  239.5420   240.2276     9570600      240.2276
2007-01-09  241.1565  242.5475  239.0452   241.1814    10832700      241.1814

3 个答案:

答案 0 :(得分:3)

下面的代码将为您提供dplyr和管道所需的功能。我不确定为什么所有事情都需要用管道来完成,因为不是每个功能都是针对magrittr管道构建的。对于as.xts,如果要使用管道,则需要使用.$引用日期列。

但是结果将不会有用。 xts转换矩阵中的数据,由于Symbol和date在矩阵中,因此整个矩阵将是字符矩阵。

library(xts)
library(dplyr)

df %>% 
  mutate(date = as.Date(date)) %>% 
  as.xts(order.by = .$date)

           symbol   date         open_pr gross  avg_aud ts    tv  
2017-10-12 "N2"     "2017-10-12" "10.2"  " 460" " 19"   " 59" " 6"
2017-10-12 "NJ"     "2017-10-12" " 2.7"  " 121" "  3"   " 32" " 1"
2017-10-12 "K-Kl"   "2017-10-12" " 0.5"  "  21" "  0"   " 31" " 0"
2017-10-12 "K-P3"   "2017-10-12" " 4.5"  " 203" "  5"   " 34" " 2"
2017-10-12 "K-N"    "2017-10-12" " 2.9"  " 130" "  5"   " 57" " 2"
2017-10-12 "KP+"    "2017-10-12" " 8.1"  " 363" " 21"   " 83" " 7"
2017-10-12 "K13"    "2017-10-12" " 2.3"  " 102" "  4"   " 59" " 1"
2017-10-12 "KS"     "2017-10-12" " 1.0"  "  45" "  1"   " 28" " 0"
2017-10-12 "KTotal" "2017-10-12" "43.2"  "1946" "153"   "113" "49"
2017-10-12 "P500"   "2017-10-12" "28.5"  "1282" " 92"   "103" "29"
2017-10-12 "P800"   "2017-10-12" " 5.8"  " 262" " 10"   " 53" " 3"
2017-10-12 "P23"    "2017-10-12" " 6.7"  " 303" " 14"   " 69" " 5"
2017-10-12 "P55"    "2017-10-12" " 5.7"  " 256" "  6"   " 33" " 2"
2017-10-12 "PA"     "2017-10-12" " 0.1"  "   6" "  0"   "  4" " 0"
2017-10-12 "PKA"    "2017-10-12" "10.0"  " 449" " 27"   " 87" " 9"

但是,如果您想在Google底部找到类似您的示例的内容,请使用以下类似内容。

第1步是创建一个函数,以创建xts时间序列,并在列名前面加上符号。步骤2按符号分割原始数据,并创建一个列表,以将所有数据包含在命名列表中。步骤3是使用Map将功能应用于数据。之后,您可以访问my_data列表中的所有数据。

my_func <- function(x, symbol){
  index <- as.Date(x[["date"]])
  x <- x[, setdiff(colnames(x), c("symbol", "date"))]
  x <- xts::as.xts(x, order.by = index)
  colnames(x) <- paste0(symbol, ".", colnames(x))
  return(x)
}

my_data <- split(df, df$symbol)

my_data <- Map(my_func, my_data, names(my_data))

head(my_data, 2)
$`K-Kl`
           K-Kl.open_pr K-Kl.gross K-Kl.avg_aud K-Kl.ts K-Kl.tv
2017-10-12          0.5         21            0      31       0

$`K-N`
           K-N.open_pr K-N.gross K-N.avg_aud K-N.ts K-N.tv
2017-10-12         2.9       130           5     57      2

答案 1 :(得分:1)

您可以简单地编写以下代码

x <- xts(df[,c(1,3:7)],df$date)

为我工作

答案 2 :(得分:0)

这项工作可以吗?

library(lubridate)
df$date <- date(df$date)
library(timetk)
df <- tk_xts(df, date_col = date)

保持管道功能:<​​/ p>

library(lubridate)
dat <- df %>% 
  as.tbl() %>% 
  mutate(date = date(date)) %>% 
  tk_xts(date_col = date)