【问题标题】:Transform the dataframe to xts object将数据框转换为 xts 对象
【发布时间】:2023-03-14 06:11:02
【问题描述】:

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

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)
)

头(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

我的sn-p

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

【问题讨论】:

  • 如果你读到 as.xts 它接受 timeSeriestsmatrixdata.framezoo 对象,所以你的 mutatedate列不起作用。
  • this

标签: r time-series xts zoo


【解决方案1】:

这行得通吗?

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

保持管道功能:

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

【讨论】:

  • 像这样使用tk_xts 也会删除符号列。将无法识别哪些数据属于哪个符号。
【解决方案2】:

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

但结果不会有用。 xts 转换矩阵中的数据,由于符号和日期在矩阵中,整个矩阵将是一个字符矩阵。

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"

但是,如果您想在谷歌底部显示您的示例,请使用以下内容。

第 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

【讨论】:

  • 有什么办法可以在最后有一个数据框而不是列表,我试图用 purrr::map_df 替换 Map 但做不到
  • 您如何设想 data.frame?列表中的每个 xts 对象都有不同的列名。你想把所有这些列都放在一个 data.frame 中吗?
  • 你是对的。我认为他需要 xts 对象。您的解决方案看起来不错,但是,如果他想使用 highchart 来可视化他的数据,例如(交互式图表),highchart 不支持列表。实际上我之前也遇到过同样的问题。
  • 组合列表中的数据可以由Reduce(merge, my_data) 完成,这会将所有xts 对象合并为一个,保持日期对齐并具有所有列。接下来,如果需要,您可以随时将其转换为 data.frame。
【解决方案3】:

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

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

为我工作

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2011-05-21
    • 1970-01-01
    • 2016-01-07
    • 2011-05-16
    • 2018-09-03
    • 2022-01-07
    • 2016-11-03
    • 2011-06-12
    相关资源
    最近更新 更多