使用dplyr的组的最大值在TSIBLE数据帧中不起作用

使用dplyr的组的最大值在TSIBLE数据帧中不起作用,r,dplyr,tsibble,R,Dplyr,Tsibble,我正在tsibbledata包中处理gafa_股票数据帧。我想找出数据框中四种股票的最高收盘价。因为dataframe有四个股票,我想得到一个有四行的表,每行给我股票的最大值。我使用此处的说明:并编写以下代码: gafa_stock %>% group_by(Symbol) %>% summarise(maximum = max(Close)) gafa_股票数据框如下所示 str(gafa_股票)有这些结果 str(gafa_stock) tsibble [5,03

我正在tsibbledata包中处理gafa_股票数据帧。我想找出数据框中四种股票的最高收盘价。因为dataframe有四个股票,我想得到一个有四行的表,每行给我股票的最大值。我使用此处的说明:并编写以下代码:

gafa_stock %>%
   group_by(Symbol) %>%
   summarise(maximum = max(Close))
gafa_股票数据框如下所示

str(gafa_股票)有这些结果

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
str(gafa_股票)
tsibble[5032x8](S3:tbl_ts/tbl_df/tbl/data.frame)
$Symbol:chr[1:5032]“AAPL”“AAPL”“AAPL”“AAPL”。。。
$Date:Date[1:5032],格式:“2014-01-02”“2014-01-03”“2014-01-06”。。。
$Open:num[1:5032]79.47976.877.877。。。
$High:num[1:5032]79.6 79.1 78.1 78 77.9。。。
$Low:num[1:5032]78.977.276.276.877。。。
$Close:num[1:5032]7977.377.777.177.6。。。
$Adj_Close:num[1:5032]6765.565.965.465.8。。。
$Volume:num[1:5032]5.87e+079.81e+071.03e+087.93e+076.46e+07。。。
-属性(*,“键”)=tibble[4 x 2](S3:tbl_df/tbl/data.frame)
..$Symbol:chr[1:4]“AAPL”“AMZN”“FB”“GOOG”
..$.行:列表[1:4]
.. ..$ : 国际[1:1258]12345678910。。。
.. ..$ : int[1:1258]1259 1260 1261 1262 1263 1264 1265 1266 1267 1268。。。
.. ..$ : 国际[1:1258]2517251825192520122522522522522523254252522526。。。
.. ..$ : 国际[1:1258]3775377637773778377937803781378237833784。。。
.. ..@ p类型:int(0)
..-attr(*,“.drop”)=logi TRUE
-属性(*,“索引”)=chr“日期”
..-attr(*,“有序”)=logi TRUE
-属性(*,“index2”)=chr“日期”
-属性(*,“间隔”)=间隔[1:1]1D
..@.常规:逻辑正确
我的最终结果是这样的


此命令创建一个表,其中包含所有5032行和三列—Symbol、Date和标记为MAX的收盘价。我做错了什么?这是因为ts或TSIBLE数据帧的某些特殊特性吗?

如果
TSIBLE
的版本<0.9.3,我们可以首先转换为TIBLE,因为还有其他类属性

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.

我用
gafa_stock
数据尝试了你的代码,但我无法重现你的问题。我得到了一个
data.frame
,它有四行两列,最后一行是max列。重新启动R会话并重试。我添加了str(gafa_stock)并尝试重新启动R会话。但问题依然存在。即使我无法重现这个问题,我也只能得到4行数据。也许某个包屏蔽了其他函数。尝试使用
gafa\u stock%>%dplyr::group\u by(Symbol)%%>%dplyr::summary(max=max(Close))
。您的
软件包版本('tsibble')
是什么?我有
'0.9.3'
。这是可行的……但我很好奇为什么我必须将其转换为tibble?是什么阻止我以TSIBLE格式使用此代码?我问这个问题是想了解一些关于TSIBLE的知识。通常,使用TSIBLE的
summary()
等操作会返回TSIBLE。如果您想在时间维度上进行总结(因此您不再需要时间序列),您应该将
作为_tible()
删除时间属性。另外请注意@akrun,您可以使用index_var()访问tsibble的索引变量:
index_var(gafa_stock)
\35;>[1]“Date”
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.
attr(gafa_stock, "index")[1]
#[1] "Date"