【问题标题】:maximum value by groups using dplyr does not work in tsibble dataframe使用 dplyr 分组的最大值在 tsibble 数据帧中不起作用
【发布时间】:2021-02-05 23:44:11
【问题描述】:

我正在处理 tsibbledata 包中的 gafa_stock 数据框。我想找到数据框中四只股票中每只股票的最高收盘价。由于数据框有四只股票,我想得到一个有四行的表格,每行给我一只股票的最大值。我在这里使用说明:Extract the maximum value within each group in a dataframe 并编写此代码:

gafa_stock %>%
   group_by(Symbol) %>%
   summarise(maximum = max(Close))

gafa_stock 数据框看起来像这样

str(gafa_stock) 有这些结果

str(gafa_stock)
tsibble [5,032 x 8] (S3: tbl_ts/tbl_df/tbl/data.frame)
$ Symbol   : chr [1:5032] "AAPL" "AAPL" "AAPL" "AAPL" ...
$ Date     : Date[1:5032], format: "2014-01-02" "2014-01-03" "2014-01-06" ...
$ Open     : num [1:5032] 79.4 79 76.8 77.8 77 ...
$ High     : num [1:5032] 79.6 79.1 78.1 78 77.9 ...
$ Low      : num [1:5032] 78.9 77.2 76.2 76.8 77 ...
$ Close    : num [1:5032] 79 77.3 77.7 77.1 77.6 ...
$ Adj_Close: num [1:5032] 67 65.5 65.9 65.4 65.8 ...
$ Volume   : num [1:5032] 5.87e+07 9.81e+07 1.03e+08 7.93e+07 6.46e+07 ...
- attr(*, "key")= tibble [4 x 2] (S3: tbl_df/tbl/data.frame)
..$ Symbol: chr [1:4] "AAPL" "AMZN" "FB" "GOOG"
..$ .rows : list<int> [1:4] 
.. ..$ : int [1:1258] 1 2 3 4 5 6 7 8 9 10 ...
.. ..$ : int [1:1258] 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 ...
.. ..$ : int [1:1258] 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 ...
.. ..$ : int [1:1258] 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 ...
.. ..@ ptype: int(0) 
..- attr(*, ".drop")= logi TRUE
- attr(*, "index")= chr "Date"
..- attr(*, "ordered")= logi TRUE
- attr(*, "index2")= chr "Date"
- attr(*, "interval")= interval [1:1] 1D
..@ .regular: logi TRUE

而且,我的最终结果是这样的

此命令创建一个包含所有 5032 行和三列的表 - 代码、日期和标记为最大值的收盘价。我究竟做错了什么?这是因为 ts 或 tsibble 数据帧的一些特殊特性吗?

【问题讨论】:

  • 我用gafa_stock 数据尝试了您的代码,但无法重现您的问题。我得到一个data.frame,它有四行两列,最后一列是最大列。重新启动您的 R 会话并重试。
  • 我添加了 str(gafa_stock) 并尝试重新启动 R 会话。但问题依然存在。
  • 即使我无法重现该问题,我也只能得到 4 行数据。也许某些包掩盖了其他功能。尝试使用gafa_stock %&gt;% dplyr::group_by(Symbol) %&gt;% dplyr::summarise(maximum = max(Close))。你的packageVersion('tsibble') 是什么?我有‘0.9.3’

标签: r dplyr tsibble


【解决方案1】:

如果tsibble的版本是,我们可以先转换为tibble,因为还有其他类属性tbl_ts

gafa_stock %>%
    as_tibble %>%
     group_by(Symbol) %>%
       summarise(maximum = max(Close), .groups = 'drop')

-输出

# A tibble: 4 x 2
#  Symbol maximum
#  <chr>    <dbl>
#1 AAPL      232.
#2 AMZN     2040.
#3 FB        218.
#4 GOOG     1268.

在较新的版本(0.9.3)中,它无需转换即可工作

gafa_stock %>%
    group_by(Symbol) %>%
    summarise(maximum = max(Close), .groups = 'drop')
# A tibble: 4 x 2
#  Symbol maximum
#  <chr>    <dbl>
#1 AAPL      232.
#2 AMZN     2040.
#3 FB        218.
#4 GOOG     1268.

根据tsibble(0.9.2)

每个观察值都应由有效 tsibble 中的索引和键唯一标识。

这里,index 的属性是“日期”

attr(gafa_stock, "index")[1]
#[1] "Date"

【讨论】:

  • 这行得通....但我很好奇为什么我必须将它转换为 tibble?是什么阻止我以 tsibble 格式使用此代码?我问这个问题是为了尝试了解 tsibble。
  • 一般summarise()等带有tsibble的操作都会返回一个tsibble。如果您想在时间维度上进行汇总(因此您不再需要时间序列),您应该使用as_tibble() 删除时间属性。
  • 另请注意@akrun,您可以使用 index_var() 访问 tsibble 的索引变量:index_var(gafa_stock)#&gt; [1] "Date"
【解决方案2】:

我想这就是你想要的:

gafa_stock %>%
  group_by(Symbol) %>% 
  filter(Close == max(Close))

结果:

# A tsibble: 4 x 8 [!]
# Key:       Symbol [4]
# Groups:    Symbol [4]
  Symbol Date        Open  High   Low Close Adj_Close   Volume
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
1 AAPL   2018-10-03  230.  233.  230.  232.      230. 28654800
2 AMZN   2018-09-04 2026. 2050. 2013  2040.     2040.  5721100
3 FB     2018-07-25  216.  219.  214.  218.      218. 58954200
4 GOOG   2018-07-26 1251  1270. 1249. 1268.     1268.  2405600

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2023-03-19
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多